BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
r_trivedi
Calcite | Level 5

Hi!

 

I'm using SAS 9.4 and I'm trying to recode a variable called PHQ_9_Severity into "Normal" "Mild" "Moderate" and "Severe". The original data set has "Normal" "Mild" "Moderate" "Moderately Severe" and "Severe". I'm trying to make "Moderately Severe" and "Severe" output as "Severe". When I run this code, it prints all of my observations but changes all of the observations that are "Mild" "Moderately Severe" and "Severe" to "Severe" and leaves "Normal" as is leaving me with only two groups.

 

data nallergy.phq;
set nallergy.phq9;
if PHQ_9_Severity = "Mild" then PHQ9Severity="Mild";
if PHQ_9_Severity = "Moderate" then PHQ9Severity="Moderate";
if PHQ_9_Severity = "Normal" then PHQ9Severity="Normal";
else PHQ9Severity = "Severe";
run;

 

I can't figure out what I'm doing wrong. Thank you in advance!!

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

You need ELSE in a number of places, so that the last line only executes if all the other conditions fail. If you only put ELSE at the end, you get incorrect results.

 

if PHQ_9_Severity = "Mild" then PHQ9Severity="Mild";
else if PHQ_9_Severity = "Moderate" then PHQ9Severity="Moderate";
else if PHQ_9_Severity = "Normal" then PHQ9Severity="Normal";
else PHQ9Severity = "Severe";
--
Paige Miller

View solution in original post

3 REPLIES 3
VDD
Ammonite | Level 13 VDD
Ammonite | Level 13

have you tried using compress and upcase to the variables to confirm that you match case sensitivity and that there are not trailing spaces?

 

posting sample data in a data step helps because we don't see what you are looking at based on your statement.

 

 

 

PaigeMiller
Diamond | Level 26

You need ELSE in a number of places, so that the last line only executes if all the other conditions fail. If you only put ELSE at the end, you get incorrect results.

 

if PHQ_9_Severity = "Mild" then PHQ9Severity="Mild";
else if PHQ_9_Severity = "Moderate" then PHQ9Severity="Moderate";
else if PHQ_9_Severity = "Normal" then PHQ9Severity="Normal";
else PHQ9Severity = "Severe";
--
Paige Miller
ballardw
Super User

You may not even need a new variable. Consider:

 

proc format library=work;
value $sev
"Normal" ="Normal"
"Mild"   ="Mild"
"Moderate"="Moderate" 
"Moderately Severe","Severe"="Severe"
;
run;


proc freq data=nalergy.phq9;
   table PHQ_9_Severity ;
   format PHQ_9_Severity $sev.;
run;

Groups created by format levels are honored by almost all of the SAS analysis, reporting or graphing procedures.

One big advantage of custom format like this is that you do not need to keep adding variable. If latter you want to analyze on three groups such as "Normal" "Mild/Moderate" and "Moderately Severe/Sever" or "Normal" "Mild" "Moderate/Moderately Severe" "Severe" you only need to create a new format.

Another advantage is if you have multiple variables that have the same coding scheme you can apply the format to those variables as well with a single format statement. Or if you need the different levels in analysis then one format to one variable and a different to others.

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 758 views
  • 3 likes
  • 4 in conversation