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.
@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
one is for the macro processor and the other is for compiler. It's all about timing just like in life 🙂
RTM- macro tokenisation process
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.
@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
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.