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.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.