The Data step is a very important step in writing the SAS code. It is actually the first step and there are tens of different ways to tell SAS to read or import the data from your source. I have only presented three examples of this step here that have been most useful for my own research projects where I had to call in the data from an external source like text file or excel spreadsheets.
Here are different templates for the DATA step you can use. You may create a library first in which you can store your data.
libname MyLibraryName "C:\Users\Mike\";
1. Inputting the data manually
/* Approach 1 – Manually entering the data into SAS */
Data dataname1;
input var1 $ var2 var3;
datalines;
var1value1 var2value1 var3value1
var1value2 var2value2 var3value2
var1value3 var2value3 var3value3
var1value4 var2value4 var3value4
;
2. Using the INFILE statement
/* Approach 2 */
Data MyLibraryName.dataname6;
set dataname5;
var4 = var1 + var2;
if var3 < 10 then delete;
if var2 >= 3 then var4 = "yes";
keep var1 var2 var3 var4;
3. Using PROC IMPORT
/* Approach 3 */
Data dataname12;
infile 'C:\Users\Mike\MyData.txt' delimeter=',' dsd;
length var1 $12 var2 $9;
input var1 $ var2 $ var3 var4 var5 @@;
var6=var1*100+var2*10+var3;
if var1 =99 then var1=.;
if var2 ~= 1 then
if var2 ~= 2 then
if var2 ~= 3
then do; v4dummy = 1; v1dummy = 0; v3dummy = 0; end;
else do; v4dummy = 0; v1dummy = 0; v3dummy = 0; end;
else do; v4dummy = 0; v1dummy = 0; v3dummy = 1; end;
else do; v4dummy = 0; v1dummy = 1; v3dummy = 0; end;
if var2 = 1 and var3 = 2 and var4 = 5
then do var6 = 1;
select (var3);
when (1) var7 =1;
when (2) var8 =1;
when (3) var9 =1;
when (4) var10 =1;
end;
end;
label var1="My label 1"
var2="My label 2"
format var1 var1Fmt. var2 var2Fmt. ;
run;