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

I have a dataset that I transposed. My goal is to identify whether each individual had any presence of several diseases in any visit. This is how the data is set up:

IDComorbVisit1Visit2Vsit3Visit4
1Diabetes0000
1Stroke0111
1Osteoar0100
2Diabetes1111
2Stroke0000
2Osteoar0011

I have more comorbidities and more visits in my dataset, this is abbreviated.

My idea was to create a list of the comorbidities so that I can loop through each one and find the max of all the visits. If there is a 1 in any of the columns, then that person gets a 1 for that disease. I'm trying to make a macro but it's not working, this is what I have so far:

%let visits= visit0, visit1, visit2, visit3, visit4;
%let name_list=Diabetes Stroke Osteoar;
%macro get_comorbidities(comorb_name=);

		%local i next_name;
		%do i=1 %to %sysfunc(countw(&name_list));
   			%let next_name = %scan(&name_list, &i);
				data try1;
					set comorb_t;
   					%if comorb=&next_name %then &comorb_name=max(&visits);
					
				run;
		%end;
%mend get_comorbidities;

%get_comorbidities(comorb_name=diabetes_flag);

But the new variable (diabetes in this case) isn't getting created. My macro understanding is still basic so I appreciate any help!

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

Then I wouldn't bother with a macro - much too messy.  Use arrays:

 

data comorb_max (drop=i);
	set comorb_t;
    array morb_vals {3} $10 _temporary_ ("diabetes","osteoar","stroke");
    array morb_vars {3} diabetes osteoar stroke ;

    do i=1 to dim(morb_vars);
      if comorb=morb_vals{i} then morb_vars{i}=max(of visit:);
    end;
run;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

6 REPLIES 6
Reeza
Super User
What are you expecting as your final output?
mkeintz
PROC Star

I suggest you write one iteration of your code WITHOUT using macro coding.  Make sure it works, then post it here, and we can help you learn how to macro-ize it  (and whether to macro-ize it)..

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
alpine_nights
Fluorite | Level 6

For each individual, I would like them to have a variable for each disease. For example:

IDComorbVisit1Visit2Vsit3Visit4DiabetesStrokeOsteoar
1Diabetes00000..
1Stroke0111.1.
1Osteoar0100..1
2Diabetes11111..
2Stroke0000.0.
2Osteoar0011..1

and this is my "unmacro-ized code";

data comorb_max;
	set comorb_t;

	if comorb="diabetes" then diabetes=max(&visits);
	if comorb="osteoar" then osteoar=max(&visits);
	if comorb="stroke" then stroke=max(&visits);
run;

The reason I thought to use a macro to loop through the comorbidity names is because I actually have ~15 of them, so the code looks messy.

I would eventually like to get each row to be one individual with the 3 variables of diseases, so like this:

IDDiabetesStrokeOsteoar
1011
2101

So now I'm wondering if my approach is not efficient at all - even without the macro.

mkeintz
PROC Star

Then I wouldn't bother with a macro - much too messy.  Use arrays:

 

data comorb_max (drop=i);
	set comorb_t;
    array morb_vals {3} $10 _temporary_ ("diabetes","osteoar","stroke");
    array morb_vars {3} diabetes osteoar stroke ;

    do i=1 to dim(morb_vars);
      if comorb=morb_vals{i} then morb_vars{i}=max(of visit:);
    end;
run;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
alpine_nights
Fluorite | Level 6

I'm not sure why I forgot about arrays - maybe I was just trying to be fancy with macros! Anyway, thank you!

PGStats
Opal | Level 21

No macro required. You could do:

 

data have;
input ID  Comorb :$16.  Visit1  Visit2  Visit3   Visit4;
datalines;
1   Diabetes    0   0   0   0
1   Stroke  0   1   1   1
1   Osteoar 0   1   0   0
2   Diabetes    1   1   1   1
2   Stroke  0   0   0   0
2   Osteoar 0   0   1   1
;

proc transpose data=have out=temp;
by id;
id comorb;
var visit:;
run;

proc summary data=temp;
by id;
var diabetes -- osteoar;
output out=want(drop=_:) max=;
run;

proc print data=want noobs; run;

PGStats_0-1614899951964.png

 

PG

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 6 replies
  • 1008 views
  • 0 likes
  • 4 in conversation