Hello,
I have a continuous variable (age) and I would like to create a new variable (age2) which is a categorical (0-20; 21-40;41-60;61-80;81-100). I used the format but it doesn't work. Could you help me ?
Thanks
R
You initialize AGECAT=., which defines the variable as a numeric variable. But then you try to assign AGECAT="0-19", which is a character string.
The first error message gives you the answer. You cannot have a format name that ends in a number. So AGE2 cannot be a format name, but AGETWO is fine.
May I suggest that instead of "1", "2", etc., you use more meaningful formats, such as
value agetwo 0-20="0-20" 21-40="21-40" ... ;
Also note that there is no semi-colon between the format levels.
Thank you.
I have this now. Do you know what is wrong please ?
A format specification needs to include the period otherwise the SAS compiler will see that name as the name of a variable instead of as a reference to a format.
format age agecat. ;
The syntax you currently have does not include a format specification:
format age agecat ;
So it will remove any formats that were already attached to the AGE and AGECAT variables. If the variable AGECAT does not exist then it will be created as numeric and will have missing values on all observations.
Please read the last sentence of my earlier reply.
Formats do not add a new variable. They change the way the variable appears to us humans, and they change the way most SAS PROCs work with the variable. So most SAS PROCs will use your continuous variable as if it was categories.
Thank you.
I tried this code but I only have . in the "agecat" column. Any thoughts ?
@Rodrigue wrote:
Thank you.
I tried this code but I only have . in the "agecat" column. Any thoughts ?
Important concept: READ THE LOG.
And if you can't really understand what it is telling you, then SHOW US the log. COpy and paste the log as text (not as a screen capture) into the window that appears when you click on the </> icon. (No more screen captures, we want text, posted in the appropriate code or text box)
I can't copy paste the log but only do screen shots. Sorry. I understand it is a problem of character/numeric data. Isn't it ?
Do the notes in the log give you a clue as to what the problem is?
Why have you switched from using FORMATs to this method?
You initialize AGECAT=., which defines the variable as a numeric variable. But then you try to assign AGECAT="0-19", which is a character string.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.