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:
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:
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.
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.
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.
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.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.