BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
User_Help
Obsidian | Level 7

Hi SAS Experts,

Trying to Proc Format A1-A5, unfortunately couldn't generate as per described with following errors.

What's wrong with the SAS code?

Helps.....

 

 

 

data a;
	infile '/folders/myfolders/a.txt';
	input ID $3. Gender $ age salary A1 A2 A3 A4 A5;
run;

proc format;
value $gender 'M' = 'Male'
			  'F' = 'Female'
			  ' ' = 'Not Entered'
			other = 'Miscoded';
			
value age low-29  = 'Less than 30'
          30-50   = '30 to 50'
          51-high = '51+';
          
value A1      1 = 'Strongly Disagree' 
			  2 = 'Disagree'
			  3 = 'No Opinion'
			  4 = 'Agree'
			  5 = 'Strongly Agree';

proc print data=a noobs;
var id gender age salary A1-A5;
format 
	   gender $gender.
	   age    age.
	   salary dollar11.2
	   A1 A1.;	   
run;

 

 

 ID Gender age salary A1 A2 A3 A4 A5

001MaleLess than 30$28,000.0012123
002Female51+$76,123.0045211
003Male30 to 50$36,500.0022221
004Female51+$128,000.0053224
005MaleLess than 30$23,060.0033342
006Male51+$90,000.0023543
007Female30 to 50$76,100.0053433

 

 

Below is the Log Results:-

 

1 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
72
73 data a;
74 infile '/folders/myfolders/a.txt';
75 input ID $3. Gender $ age salary A1 A2 A3 A4 A5;
76 run;
 
NOTE: The infile '/folders/myfolders/a.txt' is:
Filename=/folders/myfolders/a.txt,
Owner Name=root,Group Name=vboxsf,
Access Permission=-rwxrwx---,
Last Modified=29Oct2019:20:16:44,
File Size (bytes)=183
 
NOTE: 8 records were read from the infile '/folders/myfolders/a.txt'.
The minimum record length was 0.
The maximum record length was 25.
NOTE: SAS went to a new line when INPUT statement reached past the end of a line.
NOTE: The data set WORK.A has 7 observations and 9 variables.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds
 
 
77
78 proc format;
79 value $gender 'M' = 'Male'
80 'F' = 'Female'
81 ' ' = 'Not Entered'
82 other = 'Miscoded';
NOTE: Format $GENDER is already on the library WORK.FORMATS.
NOTE: Format $GENDER has been output.
83
84 value age low-29 = 'Less than 30'
85 30-50 = '30 to 50'
86 51-high = '51+';
NOTE: Format AGE is already on the library WORK.FORMATS.
NOTE: Format AGE has been output.
87
88 value A1 '1' = 'Strongly Disagree'
ERROR: The format name A1 ends in a number, which is invalid.
89 2 = 'Disagree'
90 3 = 'No Opinion'
91 4 = 'Agree'
92 5 = 'Strongly Agree';
NOTE: The previous statement has been deleted.
93
 
NOTE: The SAS System stopped processing this step because of errors.
NOTE: PROCEDURE FORMAT used (Total process time):
real time 0.00 seconds
cpu time 0.01 seconds
 
 
 
94 proc print data=a noobs;
95 var id gender age salary A1-A5;
96 format
97 gender $gender.
98 age age.
99 salary dollar11.2
100 A1 A1.;
101
102
103 run;
 
NOTE: There were 7 observations read from the data set WORK.A.
NOTE: PROCEDURE PRINT used (Total process time):
real time 0.09 seconds
cpu time 0.09 seconds
 
 
104
105 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
117
 
1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

What it says. Since you need to be able to append a width to the format name where used, the name itself MUST NOT end with a number. Use an additional underline to prevent this:

value A1_
  1 = 'Strongly Disagree' 
  2 = 'Disagree'
  3 = 'No Opinion'
  4 = 'Agree'
  5 = 'Strongly Agree'
;

View solution in original post

2 REPLIES 2
Kurt_Bremser
Super User

What it says. Since you need to be able to append a width to the format name where used, the name itself MUST NOT end with a number. Use an additional underline to prevent this:

value A1_
  1 = 'Strongly Disagree' 
  2 = 'Disagree'
  3 = 'No Opinion'
  4 = 'Agree'
  5 = 'Strongly Agree'
;
ballardw
Super User

An example of why the SAS proc format doesn't allow numbers at the end of the format:

proc format library=work;
value $gender 
   'M' = 'Male'
   'F' = 'Female'
   ' ' = 'Not Entered'
   other = 'Miscoded'
;
run;			

data example;
  input x $;
datalines;
M
F
.
pdq
;

proc print data=example;
   format x $gender3.;
run;

The general rules for using a format is the displayed length of the value. Even when you assign a longer value by default you can override that default by specifying when the format is actually used.