BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
DocMartin
Quartz | Level 8

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;

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

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.

 

View solution in original post

3 REPLIES 3
FreelanceReinh
Jade | Level 19

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.

 

ballardw
Super User

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.

PGStats
Opal | Level 21
"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. 

 

PG

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1465 views
  • 0 likes
  • 4 in conversation