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

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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