I have 100,000 patients, each of which receives one of 116 diagnoses. I'm exploding the 116 diagnoses into 116 binary variables. Some of these diagnoses are rare, so I want to roll them up into an diagnosis that is much more prevalent, and pare down the list of 116 to around 30 common diagnoses. I'm using the code below to try to do that, but the %if condition always returns a FALSE. That should not be the case for patients with rare diagnoses. Do you know what is wrong with my code?
Thanks!
Andrew
options minoperator;
%macro aggdx(dxlist, rolled_up_dx);
%if diagnosis in &dxlist %then &rolled_up_dx = 1;
%mend;
data hosp1; set hosp2;
/* Example of what I want to do:
if acidbase = 1 or comametu = 1 or diabetic = 1 then metamisc = 1;
*/
%aggdx(ACIDBASE COMAMETU DIABETIC, METAMISC)
/* So I'll have about 30 calls to %aggdx, one for each of the common diagnoses */ ;
run;
Hello @DocMartin,
You are confusing data step and macro logic: %if diagnosis in &dxlist ... checks whether the string diagnosis (not the value of a data step variable of this name!) is contained in the value of the first macro parameter (ACIDBASE ... in your example).
So, if diagnosis is a character variable in dataset HOSP2, containing diagnoses like "ACIDBASE" etc., the following macro definition should work:
%macro aggdx(dxlist, rolled_up_dx);
if diagnosis in ("%sysfunc(tranwrd(&dxlist,%str( )," "))") then &rolled_up_dx = 1;
%mend;
Now, IF/THEN as well as IN are data step code generated by the macro. In particular, the option MINOPERATOR is not necessary for this to work.
If you want to use your binary variables instead of the character variable DIAGNOSIS, the macro code will be different, of course.
Hello @DocMartin,
You are confusing data step and macro logic: %if diagnosis in &dxlist ... checks whether the string diagnosis (not the value of a data step variable of this name!) is contained in the value of the first macro parameter (ACIDBASE ... in your example).
So, if diagnosis is a character variable in dataset HOSP2, containing diagnoses like "ACIDBASE" etc., the following macro definition should work:
%macro aggdx(dxlist, rolled_up_dx);
if diagnosis in ("%sysfunc(tranwrd(&dxlist,%str( )," "))") then &rolled_up_dx = 1;
%mend;
Now, IF/THEN as well as IN are data step code generated by the macro. In particular, the option MINOPERATOR is not necessary for this to work.
If you want to use your binary variables instead of the character variable DIAGNOSIS, the macro code will be different, of course.
IN doesn't work quite that way. It want's explicit values. If you were doing a comparison with another variable created that held the name of the diagnoses then the names would have to be quoted.
If all of your codes are binary you could test to see if the MAXIMUM of any of the values is 1 for a similar comparison
so
If Max( ACIDBASE, COMAMETU, DIABETIC) = 1 then metamisc = 1;
would be equivalent. Which requires a comma delimited list. So this may work for you:
%macro aggdx(dxlist, rolled_up_dx);
%let Dxlist= %sysfunc(translate(&dxlist,%str(,),%str( )));
if max(&dxlist)=1 then &rolled_up_dx = 1;
%mend;
You were also confusing MACRO comparisons %if /%then with the desired Datastep code if then.
"Do you know what is wrong with my code?"
Yes, you are using macros.
You simply need to build a dataset with Diagnosis - Common Diagnosis equivalences and use it to recode your observations. The last part can be done with a datastep MERGE operation, a format recode, or a SQL join.
Keep macros for cases where everything else fails.
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.
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.