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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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
  • 593 views
  • 0 likes
  • 2 in conversation