I am trying to understand the output of the following proc format;
This is the proc format,
/****************************************************************
* Define a format to replicate the database code values
****************************************************************/
proc format;
value dirlabel
-1001 = 'N'
-1002 = 'NNE'
-1003 = 'NE'
-1004 = 'ENE'
-1005 = 'E'
-1006 = 'ESE'
-1007 = 'SE'
-1008 = 'SSE'
-1009 = 'S'
-1010 = 'SSW'
-1011 = 'SW'
-1012 = 'WSW'
-1013 = 'W'
-1014 = 'WNW'
-1015 = 'NW'
-1016 = 'NNW';
run;
When the following line of code is run when prevwnd == -1017, the wndclimoprevaidirectcode is set to *** (3 asteriks)
wndclimoprevaidirectcode = put(prevwnd,dirlabel.);
Is that because the -1017 is not in the proc format as a value? Is this what it is supposed to do?
If not explicitly defined SAS will create a variable/max string length with the max length of your labels. In your format that's 3.
Key value -1017 is not defined with your format and though the format will return the unformatted value. Because this value needs a length of 5 for printing it doesn't fit into a string with a length of 3 and that's why SAS prints *** instead.
To define a different length for other cases:
A. Define the length when applying the format
or B. Define a default length with your format that will take effect if no length has been specified when applying the format
or C. define an other case with your format where you define how such values should get printed
With above other format the string will become at least 16 characters. If you know they are never longer than 5 characters use format F5. instead.
From now on, please show us the ENTIRE data step code you are using, rather than just one isolated line.
The result you are getting is correct. Format dirlabel has a width of 3, and -1017 does not fit in a format with a width of 3. However, a width of 5 does not result in asterisks, if this is what you want.
data want;
prevwnd=-1017;
wndclimoprevaidirectcode = put(prevwnd,dirlabel5.);
run;
If not explicitly defined SAS will create a variable/max string length with the max length of your labels. In your format that's 3.
Key value -1017 is not defined with your format and though the format will return the unformatted value. Because this value needs a length of 5 for printing it doesn't fit into a string with a length of 3 and that's why SAS prints *** instead.
To define a different length for other cases:
A. Define the length when applying the format
or B. Define a default length with your format that will take effect if no length has been specified when applying the format
or C. define an other case with your format where you define how such values should get printed
With above other format the string will become at least 16 characters. If you know they are never longer than 5 characters use format F5. instead.
How are you looking at the "results"?
If looking at the data set viewer, make the column wider. The default 3 characters is just a bit narrow for the way the data viewer displays stuff.
When I run this code, to generate all the values used by the format:
proc format out=dirlabelout; value dirlabel -1001 = 'N' -1002 = 'NNE' -1003 = 'NE' -1004 = 'ENE' -1005 = 'E' -1006 = 'ESE' -1007 = 'SE' -1008 = 'SSE' -1009 = 'S' -1010 = 'SSW' -1011 = 'SW' -1012 = 'WSW' -1013 = 'W' -1014 = 'WNW' -1015 = 'NW' -1016 = 'NNW'; run; data junk; do x=-1016 to -1001; put x= dirlabel.; end; run;
The Log shows this result of "putting" x with the format:
x=NNW x=NW x=WNW x=W x=WSW x=SW x=SSW x=S x=SSE x=SE x=ESE x=E x=ENE x=NE x=NNE x=N
@Bob9 wrote:
But when -1017 runs I get 3 asterisks (***)
Yes, that is correct. I already explained why this happens and how to fix it.
@Bob9 wrote:
But when -1017 runs I get 3 asterisks (***)
I showed my exact code and the result, which does not match your claim.
So show me your complete code with actual data.
And you still haven't mentioned where you are looking. Log? Proc Print? Results window? Data set viewer? Exported to Excel or some other file format? And how.
In programming details matter.
/*
Make your default length of format be longer by DEFAULT= option.
*/
proc format;
value dirlabel(default=80)
-1001 = 'N'
-1002 = 'NNE'
-1003 = 'NE'
-1004 = 'ENE'
-1005 = 'E'
-1006 = 'ESE'
-1007 = 'SE'
-1008 = 'SSE'
-1009 = 'S'
-1010 = 'SSW'
-1011 = 'SW'
-1012 = 'WSW'
-1013 = 'W'
-1014 = 'WNW'
-1015 = 'NW'
-1016 = 'NNW'
;
run;
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.