DATA Step, Macro, Functions and more

creating new variable with macro

Reply
N/A
Posts: 1

creating new variable with macro

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.
Occasional Contributor BPD
Occasional Contributor
Posts: 12

Re: creating new variable with macro

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
Ask a Question
Discussion stats
  • 1 reply
  • 102 views
  • 0 likes
  • 2 in conversation