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

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

Capture.JPG

 

 

 

 

 

 

 

 

 

Capture2.JPG

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

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.

 

View solution in original post

12 REPLIES 12
PaigeMiller
Diamond | Level 26

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.

 

--
Paige Miller
Rodrigue
Obsidian | Level 7

Thank you. 

 

I have this now. Do you know what is wrong please ?

 

Capture3.JPG

Tom
Super User Tom
Super User

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.

PaigeMiller
Diamond | Level 26

Please read the last sentence of my earlier reply.

--
Paige Miller
Rodrigue
Obsidian | Level 7
Thank you. Sorry for not reading completely.
Now the program runs perfectly but I don't have the new variable in "try". How I can add the new column "agecat" to the dataset ?
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
Rodrigue
Obsidian | Level 7

Thank you.

I tried this code but I only have . in the "agecat" column. Any thoughts ?

Capture4.JPG

PaigeMiller
Diamond | Level 26

@Rodrigue wrote:

Thank you.

I tried this code but I only have . in the "agecat" column. Any thoughts ?

Capture4.JPG


 

 

 

 

 

 

 

 

 

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)

--
Paige Miller
Rodrigue
Obsidian | Level 7

 

 

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 ?Capture5.JPG

 

 

PaigeMiller
Diamond | Level 26

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?

--
Paige Miller
Rick_SAS
SAS Super FREQ

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.

 

Rodrigue
Obsidian | Level 7
Thank you Rick

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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
  • 12 replies
  • 3894 views
  • 7 likes
  • 4 in conversation