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.
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: