BookmarkSubscribeRSS Feed
JUMMY
Obsidian | Level 7

So I have 7 different subsets of a SAS data sets. I am trying 

to create a program that concatenation these 7 data sets into 

one data set and then create an age group variable with 

categories 35-44, 45-54, 55-64, 65-74, >=75.

 

The following shows the code I used:

 

Proc format;

   Value agegrp low-35=“<35”

                          35-44=“35-44”

                          45-54=“45-54”

                           55-64=“55-64”

                           65-74=“65-74”

                           75-high=“75+”;

run;

 

Libname in “E:\locationofsas”;

 

Data chapter19;

set in.ch19a in.ch19b in.ch19c in.ch19d in.ch19e in.ch19f

        in.ch19g;

format agegrp agegrp.;

run;

proc print;

run; 

 

I tried running this and the new column created for age group

is filled with “.”. It is blank. How do I create a new age group?

13 REPLIES 13
JUMMY
Obsidian | Level 7

The log says “successfully”. How do I correct the series of dots created

in the new column?

SuryaKiran
Meteorite | Level 14

Format need to be applied to columns that already exist in the dataset or the new columns that are created. Does your input data have agegrp column already? If not then you will get all missing values with column created. Define it first using the PUT() function.

Thanks,
Suryakiran
novinosrin
Tourmaline | Level 20

May be you need this

Assuming you have a numeric variable age and want to create a new char variable agegroup

 

Data chapter19;

set in.ch19a in.ch19b in.ch19c in.ch19d in.ch19e in.ch19f

        in.ch19g;

agegrp=put(age,agegrp.); /*I think this is what you want*/

format age agegrp.;/*apply format for numeric variable age if you want*/

run;

proc print;

run; 

 

Karen_Horton
Obsidian | Level 7

I suspect the problem comes in that you don't seem to have a variable called agegrp in your dataset. I suspect that you probably have a variable called age.

 

By applying the format to age instead, the format will show in your dataset, but the numeric value will still be kept so you can perform calculations etc.

 

If you want to create groups using a format, you could within your datastep include a statement such as the following:

 

AGEGRP = put(age, agegrp.);

 

Hope this helps.

SuryaKiran
Meteorite | Level 14

Create new variable using PUT()

 

agegrp=put(age,agegrp.);

Thanks,
Suryakiran
CharlotteCain
Quartz | Level 8

The put function solution has been suggested twice in the same thread by @novinosrin and @Karen_Horton. Doesn't really help or make much difference in duplicating the same solution. For the benefit of wider community, I request to you kindly offer new alternatives rather. My 2 cents & Thank you!

 

 

Karen_Horton
Obsidian | Level 7
It was actually posted within seconds of each other. Can’t help if great minds think a like.
Astounding
PROC Star

A small matter, since you already received advice on the bigger issue ...

 

Your format defines 35 exactly as being "<35".  You may want to adjust either the ranges or the labels.

novinosrin
Tourmaline | Level 20

As an alternative(FWIW), you can store the agegroup in a dataset

 

data have;
input Start $ end  $ label $ hlo $;
retain Fmtname 'agegroup'     Type 'N';
cards;
low 35 <35 L
35 44  35-44 .
45 54 45-54 .
55 64 55-64 .
65 74 65-74 .
75 high 75+ H
;

proc format cntlin=have fmtlib;
run;

data want;
do age=0 to 100;
agegroup=put(age,agegroup.);
output;
end;
run;
JUMMY
Obsidian | Level 7
How do I define the ranges? I suspect that is where the error is?
Kurt_Bremser
Super User

You had an ambiguity for the value 35 in your format, so it needs to be corrected:

proc format;
value agegrp
  low-34="<35"
  35-44="35-44"
  45-54="45-54"
  55-64="55-64"
  65-74="65-74"
  75-high="75+"
;
run;

Your code, as posted, would not run, because it hat UTF quotes in it. I suspect you committed the (not so rare) mistake of moving your code through a word processor program (eg MS Word) at one stage. Don't do that, us only pure text editors like the editor in SAS or notepad++.

 

And use the proper windows for posting programming-related text here on the communities. Use the {i} button for logs and other textual data like snippets from csv files. The "little running man" icon gives you a window that adds coloring similar to the SAS Enhanced Editor.

Both windows will preserve the text as-is, inlcuding horizontal spacings. The main posting window will trash that, replace certain sequences with smileys, and so on.

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
  • 13 replies
  • 1476 views
  • 1 like
  • 7 in conversation