BookmarkSubscribeRSS Feed
Lexclarke11
Calcite | Level 5

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;

 

3 REPLIES 3
ballardw
Super User

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)

jokos
Calcite | Level 5
data WORK.IMPORT;
set WORK.IMPORT;
format AgeN $AgeFormat.;
run;

 Hello, You need to put  "$" before the format name as I wrote it above.

LeonidBatkhan
Lapis Lazuli | Level 10

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.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 4722 views
  • 0 likes
  • 4 in conversation