This is my code - Unfortunately when I tried to run the new variable, it did categorize it but it did not rename them in proc format
*Creation of a new categorical from Quantitative;
data Work.IMPORT;
set work.IMPORT;
if 1 <=age <=17 then
AgeN="1";
else if 18 <=age <=39 then
AgeN="2";
else if 40<=age <=55 then
AgeN="3";
else if age >=56 then
AgeN="4";
run;
*Step 2;
proc format;
value AgeFormat 1='Adolescent' 2='Young Adult' 3='MiddleAge'
4='Older Adult';
run;
*Step 3;
data WORK.IMPORT;
set WORK.IMPORT;
format AgeN AgeFormat.;
run;
proc freq data=Work.import;
title "Table 5 :Frequency Table of New Categorical AgeC";
tables AgeN;
run;
*Bar Chart of New Categorical Variable AgeC;
PROC sgplot DATA = work.import;
VBAR AgeC/ datalabel stat=percent fillattrs=(color=grey);
TITLE1 "Figure 7: Relative Frequency Bar Chart of New Categorical AgeC";
xaxis label='AgeC' labelattrs=(size=12);
yaxis label='Percentage of Victims Killed by Police' labelattrs=(size=12);
RUN;
Your format is for numeric values and your AGEN variable is character as shown by assigning values like "1".
I suspect that somewhere in your log you have something similar to
"Format $AgeFormat not found". To work with values like "1" "2" etc the format name must be $AgeFormat to match character values.
Here is more typical use of format
proc format; value AgeFormat 1 - 17='Adolescent' 18 - 39='Young Adult' 40 - 55='MiddleAge' 56 - high ='Older Adult'; run;
You also reference a variable AgeC that may not have been created.
Use the format with the original numeric Age variable:
PROC sgplot DATA = work.import; VBAR Age/ datalabel stat=percent fillattrs=(color=grey); format age ageformat.; TITLE1 "Figure 7: Relative Frequency Bar Chart of Age categories"; xaxis label='AgeC' labelattrs=(size=12); yaxis label='Percentage of Victims Killed by Police' labelattrs=(size=12); RUN;
One of the very nice things about most formats is that you do not need to create a new variables to create groups of values and assign a nice label. Most of the analysis, reporting and graphing procedures will use the groups created (minor exception: graphing custom picture formats with date/time/datetime values)
data WORK.IMPORT;
set WORK.IMPORT;
format AgeN $AgeFormat.;
run;
Hello, You need to put "$" before the format name as I wrote it above.
Remove double quotes in your first data step:
data Work.IMPORT;
set work.IMPORT;
if 1 <=age <=17 then
AgeN=1;
else if 18 <=age <=39 then
AgeN=2;
else if 40<=age <=55 then
AgeN=3;
else if age >=56 then
AgeN=4;
run;
because your format is defined for numeric variables. With the " " you create character variables.
Hope this helps.
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.