BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Bob9
Calcite | Level 5

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?

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

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

Patrick_0-1699499435937.png

or B. Define a default length with your format that will take effect if no length has been specified when applying the format

Patrick_1-1699499532218.png

or C. define an other case with your format where you define how such values should get printed

Patrick_0-1699500183641.png

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.

 

 

 

 

View solution in original post

8 REPLIES 8
PaigeMiller
Diamond | Level 26

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;

 

--
Paige Miller
Bob9
Calcite | Level 5
This is what I used to test it:
   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;
  data w1;    prevwnd = -1017;    wndclimoprevaidirectcode = put(prevwnd,dirlabel.);    awind = -999;    awind_code = put(awind,dirlabel.);    bwind = -2000;    bwind_code = put(bwind,dirlabel.);  run;
proc export data=w1  outfile="dirlabel_test.csv" replace;run;endsas;

OUTPUT:prevwnd,wndclimoprevaidirectcode,awind,awind_code,bwind,bwind_code-1017,***,-999,***,-2000,***
Patrick
Opal | Level 21

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

Patrick_0-1699499435937.png

or B. Define a default length with your format that will take effect if no length has been specified when applying the format

Patrick_1-1699499532218.png

or C. define an other case with your format where you define how such values should get printed

Patrick_0-1699500183641.png

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.

 

 

 

 

ballardw
Super User

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
Calcite | Level 5
But when -1017 runs I get 3 asterisks  (***)
PaigeMiller
Diamond | Level 26

@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.

--
Paige Miller
ballardw
Super User

@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.

Ksharp
Super User
/*
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;

sas-innovate-2024.png

 

Secure your spot at the must-attend AI and analytics event of 2024: SAS Innovate 2024! Get ready for a jam-packed agenda featuring workshops, super demos, breakout sessions, roundtables, inspiring keynotes and incredible networking events.

 

Register by March 1 to snag the Early Bird rate of just $695! Don't miss out on this exclusive offer. 

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 595 views
  • 0 likes
  • 5 in conversation