BookmarkSubscribeRSS Feed
lisam
Calcite | Level 5
I am trying to create dummy variables using if then statements in a simple macro (I would like them to be called d1_var and d2_var or some prefix or suffix tied to the variable name). For some reason, the new variables are not being created and I think there must be a simple explaination. Any ideas?

%macro test(var);
%if &var=1 %then d1_&var=1;
%else d1_&var=0;
%if &var=2 %then d2_&var=1;
%else d2_&var=0;
run;
%mend test;

%test(var1);
%test(var2);
etc.
1 REPLY 1
BPD
Obsidian | Level 7 BPD
Obsidian | Level 7
Lisam,

You seem to have missed the %LET from your macro assignments.

Try:

%macro test(var);
%if &var=1 %then %LET d1_&var=1;
%else %LET d1_&var=0;
%if &var=2 %then %LET d2_&var=1;
%else %LET d2_&var=0;
%mend test;

You don't need the run statement since it is part of the data step and you're code is purely Macro language.

Hope this is enough for you.

Regards,

BPD
How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 922 views
  • 0 likes
  • 2 in conversation