SAS Dummy Dataset: Do — Loop
SAS day 45: Do Loop
While creating Tables in SAS, generate a dummy dataset for shell is necessary sometimes. All Roads lead to Rome, there are many ways to create the dummy shell dataset, but can we find an elegant way?
Desired Dummy Dataset:
DO Loop+ Array Approach:
We can use the Do Loop to create dummy observations (Rows) and Array to generate dummy variables (Columns)
data dummy(drop=btox);
length pbgrade0-pbgrade3 $20;
array a[5] pbgrade0 pbgrade1 pbgrade2 pbgrade3 pbgrade4;
do trt01an=1 to 2;
do btox= 0 to 5;
btoxgrn=put(btox,8.);
output;
end;
end;
run;
[caption id=”attachment_2752" align=”alignnone” width=”500"]
Walkerssk / Pixabay[/caption]
Old School Dummy Dataset — Output:
data dummy;
length pbgrade0-pbgrade4 $20;
pbgrade0=""; pbgrade1=""; pbgrade2=""; pbgrade3=""; pbgrade4="";
trt01an=1;btoxgrn="0";output;
btoxgrn="1";output;
btoxgrn="2";output;
btoxgrn="3";output;
btoxgrn="4";output;
btoxgrn="5";output;
trt01an=2;btoxgrn="0";output;
btoxgrn="1";output;
btoxgrn="2";output;
btoxgrn="3";output;
btoxgrn="4";output;
btoxgrn="5";output;
run;
🐱Any cat catches mice is an awesome cat, it doesn’t matter which method we use, as long as it makes sense to us, and be open-minded to learn new methods!
Bonus Do-Loop Code: Filling “0” in the empty columns
%macro add0;
data result;
set result;
%do i=0 %to 4;
if grade&i=" " then grade&i="0 (0.0)";
%end;
run;
%mend;
%add0;
Please share with me if you have an alternative method!
happy SAS Coding!