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.
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.
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.
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
Change how your format is defined to include the OTHER option.
proc format;
value $a
'00'='00'
other=' '
;
run;
I think you're right. I'll mark this solved soon.
That seems to be a good practice to keep.
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.
@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 !
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!
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.
Ready to level-up your skills? Choose your own adventure.