SAS: Problem Solving 1

Today we want to provide you with some problem-resolution options for a simple situation.

Problem:

We have 3 variables that we will call Var1, Var2, Var3. Their values ranges from 1-9 and we would like to create new variables that would flag a response based on their value on any of the 3 previous variables.

Sample:

Response 7 has one (1) variable Var1, then VarFlag1 should be equal to 1

If the same response 7 has a value 3 on Var3, then the VarFlag3 should be equal to 1

Solution 1: Data step

data mydata;
input Var1, Var2, Var3;
array vars Var1 Var2 Var3;
array flags flag1-flag9;
do over vars;
if 1 <=vars<=9 then
flags[vars]=1;
end;

cards;
123
987
2.5 7 9;
run;

proc print; run;

Solution 2: array solution

array flag{*} flag1-flag9;
do j=1 to 9;
flag{j}=(index(Var1||Var2||Var3,j)>0);
end;

Solution 2: Macro solution

%macro SET_Flags(Flag,num);
%do 1=1 % to &n;
&Flag.&i=(Var=&i or Var=&i or Var=&i);
%end;
%mend Set_Flags;

Data Mydata;
%Set_Flags(Flag,5);
run;

Advertisements