I assume that what you are trying to do is actually what this code does (no macro stuff):
data TEST1;
infile datalines;
input ID $ MOD $;
length PRODUCT $ 10;
if MOD = 'N' then M1 = 'N';
else if MOD in (6,8,9) then M1 = 'Y';
else M1 = 'N';
if ID = "0001" and M1 = "Y" then PRODUCT = "SCOOTER";
else if ID = "0001" then PRODUCT = "BIKE";
else if ID = "0002" then PRODUCT = "SKATEBOARD";
datalines;
0001 N
0001 0
0001 6
0002 8
;
run;
I assume that you turned the stuff into a macro because you want to be able to call the code with different input variables. The problem you have comes from the fact that the macro code is executed BEFORE the actual datastep (the macro is simply a code generator which creates code for the data step, which is then compiled).
To get an idea of what happens, you can use the MPRINT option:
78 data TEST1;
79 infile datalines;
80 input ID $ MOD $;
81 length PRODUCT $ 10;
82
83 %PRODUCT(ID,MOD);
MPRINT(PRODUCT): if ID = "0001" and "N" = "Y" then PRODUCT = "SCOOTER";
MPRINT(PRODUCT): else if ID = "0001" then PRODUCT = "BIKE";
MPRINT(PRODUCT): else if ID = "0002" then PRODUCT = "SKATEBOARD";
84 datalines;
What happens is that the macro variable MOD in the macro is interpreted during macro execution(with the value "MOD" in the call shown, not the value of the datastep variable), not during datastep execution. The local macro variable M1 is thus always set to N.
What you may try is something like this
%MACRO PRODUCT(ID, MOD) ;
if &ID = "0001" and &mod in(6,8,9) then PRODUCT = "SCOOTER";
else if &ID = "0001" then PRODUCT = "BIKE";
else if &ID = "0002" then PRODUCT = "SKATEBOARD";
%MEND;
options mprint;
data TEST1;
infile datalines;
input ID $ MOD $;
length PRODUCT $ 10;
%PRODUCT(ID,MOD);
datalines;
0001 N
0001 0
0001 6
0002 8
;
run;
... View more