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

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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