Hello,
I have several values under the variable Comorbidities per unique studyid. I'd like to create new dummy variables for each comorbidity listed.
Data have;
Studyid Comorbidities
678 smoker, alcohol use disorder
679 current smoker
680 hypertension, diabetes mellitus
data want;
studyid Smoker Alcohol_use_disorder Hypertension Diabetes_Mellitus
678 1 1 0 0
679 1 0 0 0
680 0 0 1 1
First, dissect the string:
data long;
set have;
length comorbidity $50;
value = 1;
do i = 1 to countw(comorbidities,',');
comorbidity = translate(strip(scan(comorbidities,i,',')),'_',' ');
output;
end;
drop i comorbidities;
run;
Then you can transpose that (although I recommend to keep the long dataset, as it is easier to work with):
proc transpose
data=long
out=want (dropy=_name_)
;
by studyid;
id comorbidity;
var value;
run;
Untested, posted from my tablet.
Add the additional characters that need to be replaced to the TRANSLATE function.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.