Changing variable values using macros

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

Changing variable values using macros

I am trying to dichotomise a few variables using a 0.5 threshold. 

 

%MACRO dichot(dichVar=);

data quasi.MVNdi;
set quasi.MVN;
%if &dichVar < 0.5 %then &dichVar = 0;
%else &dichVar = 1;; /*WHY THERE HAS TO BE 2 SEMICOLONS*/
RUN;

proc print data=quasi.mdi;
var &dichVar;
run;;

%MEND dichot;

 

%dichot(dichvar=Latest_lipid_drug);
%dichot(dichvar=Latest_diabetes);

 

somehow the results that came out were all 1 (for both variables, original values are normally distributed between -1 to 1).

 

Anywhere wrong with the code? Thanks.


Accepted Solutions
Solution
3 weeks ago
Super User
Posts: 12,148

Re: Changing variable values using macros


lyfaqu wrote:

I am trying to dichotomise a few variables using a 0.5 threshold. 

 

%MACRO dichot(dichVar=);

data quasi.MVNdi;
set quasi.MVN;
%if &dichVar < 0.5 %then &dichVar = 0;
%else &dichVar = 1;; /*WHY THERE HAS TO BE 2 SEMICOLONS*/
RUN;

proc print data=quasi.mdi;
var &dichVar;
run;;

%MEND dichot;

 

%dichot(dichvar=Latest_lipid_drug);
%dichot(dichvar=Latest_diabetes);

 

somehow the results that came out were all 1 (for both variables, original values are normally distributed between -1 to 1).

 

Anywhere wrong with the code? Thanks.


Basically I do not see any need for a macro anywhere.

Any time you see a requirement to do the exact same thing to multiple variables think ARRAY instead and process all of them at once

Some thing like:

data quasi.MVNdi;
   set quasi.MVN;
   array di Latest_lipid_drug Latest_diabetes ;
   do i = 1 to dim(di);
      if not missing di[i] then di[i]= di[i] ge 0.5;
   end;
   drop i;
RUN;

Likely Problems: 1) You overwrite the output data set with each call for a single variable 2) total execution time increases with more variables. 3) You need to be careful with <  or <= comparisons about whether you want missing values included or not. Missing is < any explicit value 4) Since macro variables are compared at the compilation phase you aren't generating the code you think you are.

 

%if DOES NOT evaluate data step values but macro variable values.

 

As for why the two semicolons: run your code like this:

options mprint symbolgen;

%dichot(dichvar=Latest_lipid_drug);

to see what the macro is generating.

 

 

 

the t

View solution in original post


All Replies
PROC Star
Posts: 827

Re: Changing variable values using macros

one is for the macro processor and the other is for compiler. It's all about timing just like in life Smiley Happy

RTM- macro tokenisation process

Super User
Posts: 5,987

Re: Changing variable values using macros

[ Edited ]

In other words ...

 

macro language does not read the contents of a DATA set (in nearly all cases).

 

Use DATA step statements:

 

IF instead of %IF

THEN instead of %THEN

ELSE instead of %ELSE

 

You won't need two semicolons any longer, and best of all, you will get an accurate result.

Solution
3 weeks ago
Super User
Posts: 12,148

Re: Changing variable values using macros


lyfaqu wrote:

I am trying to dichotomise a few variables using a 0.5 threshold. 

 

%MACRO dichot(dichVar=);

data quasi.MVNdi;
set quasi.MVN;
%if &dichVar < 0.5 %then &dichVar = 0;
%else &dichVar = 1;; /*WHY THERE HAS TO BE 2 SEMICOLONS*/
RUN;

proc print data=quasi.mdi;
var &dichVar;
run;;

%MEND dichot;

 

%dichot(dichvar=Latest_lipid_drug);
%dichot(dichvar=Latest_diabetes);

 

somehow the results that came out were all 1 (for both variables, original values are normally distributed between -1 to 1).

 

Anywhere wrong with the code? Thanks.


Basically I do not see any need for a macro anywhere.

Any time you see a requirement to do the exact same thing to multiple variables think ARRAY instead and process all of them at once

Some thing like:

data quasi.MVNdi;
   set quasi.MVN;
   array di Latest_lipid_drug Latest_diabetes ;
   do i = 1 to dim(di);
      if not missing di[i] then di[i]= di[i] ge 0.5;
   end;
   drop i;
RUN;

Likely Problems: 1) You overwrite the output data set with each call for a single variable 2) total execution time increases with more variables. 3) You need to be careful with <  or <= comparisons about whether you want missing values included or not. Missing is < any explicit value 4) Since macro variables are compared at the compilation phase you aren't generating the code you think you are.

 

%if DOES NOT evaluate data step values but macro variable values.

 

As for why the two semicolons: run your code like this:

options mprint symbolgen;

%dichot(dichvar=Latest_lipid_drug);

to see what the macro is generating.

 

 

 

the t

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 127 views
  • 5 likes
  • 4 in conversation