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

I have the following code and I am not getting the intended result.

 

proc format;
value $tem
'T1'-'T53','T74'="WA"
'T54'-'T73','T75'="MB" ;
run;

 

Issue:

  1. T8 and T9 are not getting formatted as WA
  2. T6 and T7 are being formatted as MB instead of WA.

Your solutions are highly appreciated

 

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

As a string, 'T8' is greater than 'T5', and therefore also greater than 'T53'.

Use a do loop in a data step to create a cntlin dataset for proc format:

data cntlin;
fmtname = 'tem';
type = 'C';
label = 'WA';
do i = 1 to 53;
  start = 'T' !! strip(put(i,best.));
  output;
end;
run;

proc format cntlin=cntlin;
run;

Add code for the second range and the single values not in the ranges.

View solution in original post

5 REPLIES 5
Kurt_Bremser
Super User

As a string, 'T8' is greater than 'T5', and therefore also greater than 'T53'.

Use a do loop in a data step to create a cntlin dataset for proc format:

data cntlin;
fmtname = 'tem';
type = 'C';
label = 'WA';
do i = 1 to 53;
  start = 'T' !! strip(put(i,best.));
  output;
end;
run;

proc format cntlin=cntlin;
run;

Add code for the second range and the single values not in the ranges.

sri1
Obsidian | Level 7
@Kurt_Bremser Thanks ..this worked
ballardw
Super User

@sri1 wrote:

I have the following code and I am not getting the intended result.

 

proc format;
value $tem
'T1'-'T53','T74'="WA"
'T54'-'T73','T75'="MB" ;
run;

 

Issue:

  1. T8 and T9 are not getting formatted as WA
  2. T6 and T7 are being formatted as MB instead of WA.

Your solutions are highly appreciated

 

Thanks


Intervals and character values do not play well together because character values use ASCII character comparisons, not Character + partial-characters-converted-to-numeric.

Either use an explicit list or ensure that the data values will compare the way you think they should such as "T08 instead of "T8". Of course if you go to "T100" then you need "T008" to use an interval.

sri1
Obsidian | Level 7
@ballardw Thanks for response.I have tried using T01 instead of T1 in the format but didn't work.Is that what you mean

Thanks
ballardw
Super User

@sri1 wrote:
@ballardw Thanks for response.I have tried using T01 instead of T1 in the format but didn't work.Is that what you mean

Thanks

Value formats are going to have to match the actual values. If your value is "T8" then using "T08" in the format won't help.

The values would have to actually be "T08".

In which case a range may work.

Please consider:

Proc format library=work;
value $tt
"T01" - "T09"= 'abc'
"T10" - "T15"= 'pdq'
;
run;

data example;
   input v $;
datalines;
T01
T02
T03
T04
T05
T06
T07
T08
T09
T10
T11
T12
T13
T14
T15
t01
T8
T011
T101
;
run;

proc print data=example;
  format v $tt.;
run;

Note that if the letter case is different then the format doesn't find a match. That T8 does not find a match to format in the example.

 

And T011 is treated the same as T01, as T101 formatted the same as T10. These last two are because T011 > T01 as the last 1 is greater than a blank but since the third character 1 is before the 9 it is < T09 and similar for the T101.

 

So your actual values have to be very closely considered if attempting to do anything with a range of character values. Generally it is going to be more reliable with explicit value lists though ranges may be useful if you are working with things more like T01.1 to T01.999.

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1648 views
  • 0 likes
  • 3 in conversation