DATA Step, Macro, Functions and more

Questions about Proc Formats

Reply
Contributor
Posts: 21

Questions about Proc Formats

Hi there, I am reading the advanced exam prep guide and I got a few questions about proc formats. Hopefully some of you experts can help Smiley Happy

 

Question 1

 

proc format;
value $route 'Route1' = 'Zone 1'
			 'Route2' - 'Route4' = 'Zone 2'
			 'Route5' - 'Route9' = 'Zone 3';
run;

data route;
format route $route.;
do i = 1 to 9;
route = 'Route' || compress(put(i, 2.0));
output;
end;
run;

This code is similar to what the prep guide has. It defines the formats mapping routes to different zones.

 

Currently, route2 to route4 are mapped to zone 2. Now, I'm gonna make some changes to the format and map route2 to route6 to zone 2:

 

proc format;
value $route 'Route1' = 'Zone 1'
			 'Route2' - 'Route6' = 'Zone 2'
			 'Route7' - 'Route9' = 'Zone 3';
run;

data route;
format route $route.;
do i = 1 to 9;
route = 'Route' || compress(put(i, 2.0));
output;
end;
run;

I still get the previous result:

 

i2.png

 

The format wasn't updated in SAS Studio. Is this a bug in SS? I tried the exact same code on SAS 9.4 and it worked.

 

Question 2

 

For zone 2, I changed the route as route2 to route12:

 

proc format;
value $route 'Route1' = 'Zone 1'
			 'Route2' - 'Route12' = 'Zone 2'
			 'Route13' - 'Route20' = 'Zone 3';
run;

I got this error message:

 

ERROR: Start is greater than end: -.

 

Could someone explain why? Does the range have to be within single digit?

 

 

Super User
Posts: 5,079

Re: Questions about Proc Formats

For question 2 ...

 

As character strings, "Route12" is less than "Route2".  The first five characters are identical, so the sixth character determines which is greater.  For this application, you could overcome this error by changing the second range:

 

'Route2'-'Route9', 'Route10'-'Route12' = 'Zone 2'

 

In other applications, it may not be so easy.  If you can control the data, it might help to use two-digit values, such as "Route01" instead of "Route1".

Contributor
Posts: 21

Re: Questions about Proc Formats

Thanks Astounding. That makes sense.

 

 

Ask a Question
Discussion stats
  • 2 replies
  • 155 views
  • 1 like
  • 2 in conversation