SAS (Base SAS)+
data DM;
infile datalines dlm='|' dsd missover;
input subjid : $10. age : best32.;
label ;
format ;
datalines4;
1001|35
1002|64
1003|19
;;;;
run;
data dm02;
set dm;
agemon=age*12;
group=1;
run;
data dm02;
set dm;
length agegr1 $30;
if age lt 20 then agegr1="<20 Years";
else if 20 le age lt 60 then agegr1="20 - <60 Years";
else if age ge 60 then agegr1=">= 60 Years";
run;- To derive a new variable in SAS, we assign to it inside a DATA step:
BMI = weight / (height**2) * 703;. - Any name that did not exist on the left side of an assignment before becomes a new column in the output dataset — no explicit declaration needed.
- The formula uses the imperial BMI shortcut: weight (lb) divided by height (in) squared, times 703. We could equally convert to metric first and use the textbook formula.
- SAS applies the formula to every row as it reads the input, so the computation is vectorised by default.
- We could also derive many variables in one DATA step — just keep adding assignment lines before the
run;.