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.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.