BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
UcheOkoro
Lapis Lazuli | Level 10

Please, I need help creating a single categorical variables from several categorical variables. The categorical variables from which I would like to create a single variable are Agitation(1, 0), Akathisia(1, 0),  Anxiety_Depression(1, 0),  Arrhythmia(1, 0), Cough(1, 0),  Dizziness(1, 0), Home_medication(1, 0), Other(1, 0),  Pain(1, 0), Psychosis(1, 0),  Secretion(1, 0)  and High_Bld_Pressure(1, 0). They are mutually exclusive. I would like to create a variable called indication that will have the disease name as observation eg(Agitiation, Cough, Dizziness etc). The code I have below seems to exclude some  observation.

Data long232c;
set long232b;
 if Agitation=1 then Indication='Agitation';
if Akathisia=1 then Indication='Akathisia';
if Anxiety_Depression=1 then Indication='Anxiety_Dep';
if Arrhythmia=1 then Indication='Arrhythmia';
if Cough=1 then Indication='Cough';
if Dizziness=1 then Indication='Dizziness';
if Fever=1 then Indication='Fever';
if High_Bld_Pressure=1 then Indication='HBP';
if Home_medication ne . and Home_medication=1 then Indication='Home_med';
if Nausea_Vomiting=1 then Indication='Nausea_Vom';
if Other= 1 then Indication='Other';
if Pain=1 then Indication='Pain';
if Psychosis=1 then Indication='Psychosis';
if Secretion=1 then Indication='Secretion';
if Seizure=1 then Indication='Seizure';
if Sleep_disturbance=1 then Indication='Sleep_dist';
if Shortness_of_breath=1 then Indication='SOB';
if Spasm=1 then Indication='Spasm';
if Swelling=1 then Indication='Swelling';
if Withdrawal=1 then Indication='Withdrawal';run;

proc freq data=long232c;
table Indication Agitation Akathisia Anxiety_Depression Arrhythmia Cough Dizziness Home_medication Other Pain Psychosis Secretion High_Bld_Pressure;run;

UcheOkoro_0-1616533881609.pngUcheOkoro_1-1616533924859.png

 

Thank you

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
Data long232c;
set long232b;

array _issues(*) agitation --withdrawal;

length indications $512.;
call missing(indications);

do i=1 to dim(_issues);
if _issues(i) = 1 then do;
indications = catx(",", trim(indications), vname(_issues(i)));
end;
end; run;

Key concepts here are arrays, the usage of the VNAME function to get the disease from the variable name and CATX() to append the data. 

Untested but should get you started.

 


@UcheOkoro wrote:

Please, I need help creating a single categorical variables from several categorical variables. The categorical variables from which I would like to create a single variable are Agitation(1, 0), Akathisia(1, 0),  Anxiety_Depression(1, 0),  Arrhythmia(1, 0), Cough(1, 0),  Dizziness(1, 0), Home_medication(1, 0), Other(1, 0),  Pain(1, 0), Psychosis(1, 0),  Secretion(1, 0)  and High_Bld_Pressure(1, 0). They are mutually exclusive. I would like to create a variable called indication that will have the disease name as observation eg(Agitiation, Cough, Dizziness etc). The code I have below seems to exclude some  observation.

Data long232c;
set long232b;
 if Agitation=1 then Indication='Agitation';
if Akathisia=1 then Indication='Akathisia';
if Anxiety_Depression=1 then Indication='Anxiety_Dep';
if Arrhythmia=1 then Indication='Arrhythmia';
if Cough=1 then Indication='Cough';
if Dizziness=1 then Indication='Dizziness';
if Fever=1 then Indication='Fever';
if High_Bld_Pressure=1 then Indication='HBP';
if Home_medication ne . and Home_medication=1 then Indication='Home_med';
if Nausea_Vomiting=1 then Indication='Nausea_Vom';
if Other= 1 then Indication='Other';
if Pain=1 then Indication='Pain';
if Psychosis=1 then Indication='Psychosis';
if Secretion=1 then Indication='Secretion';
if Seizure=1 then Indication='Seizure';
if Sleep_disturbance=1 then Indication='Sleep_dist';
if Shortness_of_breath=1 then Indication='SOB';
if Spasm=1 then Indication='Spasm';
if Swelling=1 then Indication='Swelling';
if Withdrawal=1 then Indication='Withdrawal';run;

proc freq data=long232c;
table Indication Agitation Akathisia Anxiety_Depression Arrhythmia Cough Dizziness Home_medication Other Pain Psychosis Secretion High_Bld_Pressure;run;

UcheOkoro_0-1616533881609.pngUcheOkoro_1-1616533924859.png

 

Thank you


 

View solution in original post

3 REPLIES 3
Reeza
Super User
Data long232c;
set long232b;

array _issues(*) agitation --withdrawal;

length indications $512.;
call missing(indications);

do i=1 to dim(_issues);
if _issues(i) = 1 then do;
indications = catx(",", trim(indications), vname(_issues(i)));
end;
end; run;

Key concepts here are arrays, the usage of the VNAME function to get the disease from the variable name and CATX() to append the data. 

Untested but should get you started.

 


@UcheOkoro wrote:

Please, I need help creating a single categorical variables from several categorical variables. The categorical variables from which I would like to create a single variable are Agitation(1, 0), Akathisia(1, 0),  Anxiety_Depression(1, 0),  Arrhythmia(1, 0), Cough(1, 0),  Dizziness(1, 0), Home_medication(1, 0), Other(1, 0),  Pain(1, 0), Psychosis(1, 0),  Secretion(1, 0)  and High_Bld_Pressure(1, 0). They are mutually exclusive. I would like to create a variable called indication that will have the disease name as observation eg(Agitiation, Cough, Dizziness etc). The code I have below seems to exclude some  observation.

Data long232c;
set long232b;
 if Agitation=1 then Indication='Agitation';
if Akathisia=1 then Indication='Akathisia';
if Anxiety_Depression=1 then Indication='Anxiety_Dep';
if Arrhythmia=1 then Indication='Arrhythmia';
if Cough=1 then Indication='Cough';
if Dizziness=1 then Indication='Dizziness';
if Fever=1 then Indication='Fever';
if High_Bld_Pressure=1 then Indication='HBP';
if Home_medication ne . and Home_medication=1 then Indication='Home_med';
if Nausea_Vomiting=1 then Indication='Nausea_Vom';
if Other= 1 then Indication='Other';
if Pain=1 then Indication='Pain';
if Psychosis=1 then Indication='Psychosis';
if Secretion=1 then Indication='Secretion';
if Seizure=1 then Indication='Seizure';
if Sleep_disturbance=1 then Indication='Sleep_dist';
if Shortness_of_breath=1 then Indication='SOB';
if Spasm=1 then Indication='Spasm';
if Swelling=1 then Indication='Swelling';
if Withdrawal=1 then Indication='Withdrawal';run;

proc freq data=long232c;
table Indication Agitation Akathisia Anxiety_Depression Arrhythmia Cough Dizziness Home_medication Other Pain Psychosis Secretion High_Bld_Pressure;run;

UcheOkoro_0-1616533881609.pngUcheOkoro_1-1616533924859.png

 

Thank you


 

Reeza
Super User
I suspect you're overwriting your conditions, ie I don't think your categories are mutually exclusive. You test for COUGH but if you have COUGH AND Swelling for example, it'll only show swelling and the COUGH portion is erased because of how you're coding the variables.
You can check this with a multiway proc freq check.

proc freq data=have;
table home_medication*cough*pain*.........;
run;
UcheOkoro
Lapis Lazuli | Level 10

Yes, I noticed that were not mutually exclusive after running the code with the array statement. I was over writing the conditions. Thank you for this observation

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
  • 3 replies
  • 690 views
  • 0 likes
  • 2 in conversation