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

I have a series of formats like the one below, in different catalogs.  They are string and numeric.

 

proc format;
  value $A
    '00'="00"
    '01'="foo"
    '02'="bar"
    '03'="sna"
    '04'="fu"
run;

 

How, easy way and hard way, does one test if the put() function will return a mapped value and not just pass through the value of the first parameter that is unmatched?

 

I have faith that SAS has a function for or some syntax solution for this, yet I don't quite know how to google it here or there.  Any clue or idea would help.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Given your example NO.

Normally I would just test if the formatted value matches the unformatted value to tell if the format did anything.  But your format leaves 00 unchanged.  Also that won't work for numeric formats since everything is changed because the format will convert numbers to text.

 

You can get the definition of the format out into a dataset.

proc format lib=libref.catname cntlout=myformats; run;

If the format are simple lists like your example you could then just check if the value you have matches one of the START value for the format in question.

View solution in original post

9 REPLIES 9
Tom
Super User Tom
Super User

Given your example NO.

Normally I would just test if the formatted value matches the unformatted value to tell if the format did anything.  But your format leaves 00 unchanged.  Also that won't work for numeric formats since everything is changed because the format will convert numbers to text.

 

You can get the definition of the format out into a dataset.

proc format lib=libref.catname cntlout=myformats; run;

If the format are simple lists like your example you could then just check if the value you have matches one of the START value for the format in question.

PhilC
Rhodochrosite | Level 12

Wow, I was afraid of that.  I was hoping that there was something.  

 

I dream of a put function that does this.

data result; 
  do x = '00', 'AA'; 
    y=put(x,?$A.); *Question mark a la input();
    output;
  end;
run;

result:

 

x  y  
00 00
AA 

 

 

 

 

Tom
Super User Tom
Super User

Change how your format is defined to include the OTHER option.

proc format;
 value $a 
   '00'='00'
   other=' '
 ;
run;
PhilC
Rhodochrosite | Level 12

I think you're right.  I'll mark this solved soon.

Reeza
Super User
I use the Other = "CHECKME" in most code to have errors jump out at me...I don't always catch it but it's a pretty obvious mistake and I know where to look right away.
PhilC
Rhodochrosite | Level 12

That seems to be a good practice to keep.

ballardw
Super User

A similar concept using a custom informat when reading external data can be used either something like:

proc format;
  invalue $A
    '00','01','02','03','04'= _same_
    other=_error_
  ;
run;

and read the data using the $A. informat. If a value other than those listed is encountered you get an invalid data message and diagnostics wth the value set to missing for the non-listed values.

Or if your would actually prefer to have the formatted values as the actual value then read the data as such:

proc format;
  invalue $A
    '00'="00"
    '01'="foo"
    '02'="bar"
    '03'="sna"
    '04'="fu"
    other=_error_
  ;
run;

which again has the invalid data diagnostics for non listed values.

Reeza
Super User

@ballardw wrote:

A similar concept using a custom informat when reading external data can be used either something like:

proc format;
  invalue $A
    '00','01','02','03','04'= _same_
    other=_error_
  ;
run;

and read the data using the $A. informat. If a value other than those listed is encountered you get an invalid data message and diagnostics wth the value set to missing for the non-listed values.

Or if your would actually prefer to have the formatted values as the actual value then read the data as such:

proc format;
  invalue $A
    '00'="00"
    '01'="foo"
    '02'="bar"
    '03'="sna"
    '04'="fu"
    other=_error_
  ;
run;

which again has the invalid data diagnostics for non listed values.


You learn something new everyday, that's an awesomely useful feature, thanks @ballardw !

PhilC
Rhodochrosite | Level 12

@Reeza @ballardw @Tom ,

I'm glad I didn't close this out right after Tom responded, thanks for your contributions.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 9 replies
  • 3798 views
  • 13 likes
  • 4 in conversation