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

Hello all,

 

I am a newbie to SAS and trying to recode a categorical variable in different ways; however, none of them worked. 

I still have not figured out how to fix this problem. 

 

What I want to do is to create a new variable (4 categories) based on an old variable's responses (8 categories).

I tried several ways, but none of them worked. I attached my codes below.

 

1. I used the format and added $ before the variable name because B5 ends with the number. ( I am not sure if I understand this correctly)

proc format;
value $B5
'increased a lot'='increased'
'increased somewhat'='increased'
'increased a litte'='increased'
'stayed the same'='stayed the same'
'decreased a litte'='decreased'
'decreased somewhat'='decreased'
'decreased a lot'='decreased'
'N/A'='N/A'
;
run;

I got an error message:  

 

berylsky1_1-1612223369508.png

 

 

2. I used the if/then statement here. 

Data risk3; /**create a new dataset**/
Set risk2;
If B5 in ('stayed the same') then B5_R='stayed the same';
If B5 in ('increased a lot' 'increased somewhat' 'increased a little') then B5_R='increased';
If B5 in ('decreased a little' 'decreased somewhat' 'decreased a lot') then B5_R='decreased';
If B5 in ('N/A') then B5_R='N/A';
If B5=. then B5_R=.;
run;

When running this code, I got the long notes: 

berylsky1_0-1612223330210.png

 

 
I googled how to recode a categorical variable for a few hours and got stuck on this problem.
Please share your suggestions to solve this problem. 
 
Thank you for your help. 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

First you cannot Name a format ending in digits. If you remember things like other formats such as F the digits are the display width. So the same applies to custom formats. You could try naming your format B5_ if you really like the 5 as part of the name.

 

Second, on this forum please include the entire procedure or data step from the log when asking questions about it. Copy the entire text of the submitted code along with all the notes, warnings or other messages. Then on the forum open a text box and paste the text.

 

The note means that you have used a variable that is character in a fashion reserved for numeric variables.

I can't copy and paste as you are showing pictures and incomplete data step.

 

Part of this could come from "if B5 = . ". Your B5 appears to be character. If you want to test to see if a value is missing use the Missing function instead. Do not assign missing to character values with " = . " Either use a literal blank ='' or use the call missing function.

View solution in original post

3 REPLIES 3
PGStats
Opal | Level 21

The format name must end with a letter or an underscore, then you may use it to recode your variable

proc format;
value $B5_FORMAT
'increased a lot'='increased'
'increased somewhat'='increased'
'increased a litte'='increased'
'stayed the same'='stayed the same'
'decreased a litte'='decreased'
'decreased somewhat'='decreased'
'decreased a lot'='decreased'
'N/A'='N/A'
;
run;

Data risk3; /**create a new dataset**/
Set risk2;
length B5_R $16;
B5_R = input(B5, ?? $B5_FORMAT.);
run;

Note The ?? prevents SAS from generating warnings when the format can't be applied. It quietly produces a missing value (" ") instead.

PG
ballardw
Super User

First you cannot Name a format ending in digits. If you remember things like other formats such as F the digits are the display width. So the same applies to custom formats. You could try naming your format B5_ if you really like the 5 as part of the name.

 

Second, on this forum please include the entire procedure or data step from the log when asking questions about it. Copy the entire text of the submitted code along with all the notes, warnings or other messages. Then on the forum open a text box and paste the text.

 

The note means that you have used a variable that is character in a fashion reserved for numeric variables.

I can't copy and paste as you are showing pictures and incomplete data step.

 

Part of this could come from "if B5 = . ". Your B5 appears to be character. If you want to test to see if a value is missing use the Missing function instead. Do not assign missing to character values with " = . " Either use a literal blank ='' or use the call missing function.

berylsky1
Fluorite | Level 6

Thank you for your help! I found the error and changed it, and based on your advice; I will include the entire data step for the next question. 

 

 

Ready to join fellow brilliant minds for the SAS Hackathon?

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!
What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 1028 views
  • 2 likes
  • 3 in conversation