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?
What does the log from that data step say?
The log says “successfully”. How do I correct the series of dots created
in the new column?
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.
Sorry, I was not precise enough. Please post the log from that step. Use the {i} icon.
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;
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.
Create new variable using PUT()
agegrp=put(age,agegrp.);
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!
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.
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;
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.