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 🙂
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:
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?
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".
Thanks Astounding. That makes sense.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.