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

HI,

I have binned an age variable for example the age=41 to a category of 40-45 using the proc format method. The variable name is used as Age_bin, In this format the variable is still numeric. However I want to convert it into a character type of "40-45". however when I use Age_bin1 = put(Age_bin, 8.); it outputs as "41" and not the character name "40-45". Is there a simple way to do this?

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

So not a formatting issue at all.

 

I see two possible approaches 

 

Approach #1

In SQL, something like this — if the numeric variable in data set 1 is called age and you have a format called agef. for it, and the character variable in data set 2 is called age2

 

on put(age,agef.)=age2

 

Approach #2

Do not use formats for this purpose (they are still valuable for other purposes) and create the character variable age_bin.

 

Since Approach #1 does not require creating a new variable, I vote for Approach #1

 

Please, in the future, please explain WHY you are doing things, don't just say this is what you want the code to do, explain WHY you are doing it. Without that explanation, often we cannot get to a solution that is helpful to you.

--
Paige Miller

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

@eroolpal wrote:

HI,

I have binned an age variable for example the age=41 to a category of 40-45 using the proc format method. The variable name is used as Age_bin, In this format the variable is still numeric. However I want to convert it into a character type of "40-45". however when I use Age_bin1 = put(Age_bin, 8.); it outputs as "41" and not the character name "40-45". Is there a simple way to do this?


I think you have missed the point. Formats change the appearance without changing the underlying value. So since the underlying value is numeric, applying a format keeps the variable as numeric. Furthermore, you don't need a new variable age_bin, you just apply the format to age.

 

What benefit will there be to creating a character variable here? Please explain why you want a character variable. Keeping this as numeric will allow the values to sort numerically; whereas a character variable will sort alphabetically, which often is not what you want.

--
Paige Miller
eroolpal
Fluorite | Level 6

Thanks for the response and the info. I have another table with age as categories 30-35, 40-45 etc and I wanted to left join with a column with the same categories

PaigeMiller
Diamond | Level 26

So not a formatting issue at all.

 

I see two possible approaches 

 

Approach #1

In SQL, something like this — if the numeric variable in data set 1 is called age and you have a format called agef. for it, and the character variable in data set 2 is called age2

 

on put(age,agef.)=age2

 

Approach #2

Do not use formats for this purpose (they are still valuable for other purposes) and create the character variable age_bin.

 

Since Approach #1 does not require creating a new variable, I vote for Approach #1

 

Please, in the future, please explain WHY you are doing things, don't just say this is what you want the code to do, explain WHY you are doing it. Without that explanation, often we cannot get to a solution that is helpful to you.

--
Paige Miller
Quentin
Super User

Please show the code for a small example.  Show the code you use to create the Age_bin format.  Also show an example DATA step using the format which gives the result you don't like.  Your text description is a little confusing.  Typically if you use a format to create a character variable age_bin from a numeric variable age it would look like:

 

data want ;
  age=41 ;
  Age_bin = put(Age, age_bin.) ;
run;

 

BASUG is hosting free webinars Next up: Don Henderson presenting on using hash functions (not hash tables!) to segment data on June 12. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
Patrick
Opal | Level 21

@eroolpal wrote:

HI,

I have binned an age variable for example the age=41 to a category of 40-45 using the proc format method. The variable name is used as Age_bin, In this format the variable is still numeric. However I want to convert it into a character type of "40-45". however when I use Age_bin1 = put(Age_bin, 8.); it outputs as "41" and not the character name "40-45". Is there a simple way to do this?


The internal value of your numerical age variable is always a number like 41. A format applied to such a variable only changes how the internal value gets printed.

If you want to assign the printed value to another character variable like your Age_bin1 then you need to use the format in your put statement - ie. Age_bin1 = put(age,<your format>.)

The put function writes the internal value with the format applied (=the formatted value) and that's what then gets assigned to the variable on the left side of the equal sign.

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!
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
  • 5 replies
  • 699 views
  • 6 likes
  • 4 in conversation