Normally when you use put(var, format), it will either return the corresponding label or the original value if there is no match in the format. However, returning the original value defeats the purpose of using user-defined formats in many cases.
Does anybody know if there's a "clean" or "built-in" way to force the put statement (or some other function) to return a blank or null instead of the original value when the format has no match?
For example, if we're using a zip to city format called $zip_city:
data test;
input zip $5.;
datalines;
43765
;
run;
data test2;
set test;
city = put(zip, $zip_city.);
run;
The above code returns city = 43765 (since that zip does not exist in the US yet), but I'd like a blank or null returned. I can always force a blank return with an if statement (if put(zip, $zip_city.)=zip then city=.), but if anybody can enlighten me with a better way I'd really appreciate it!
SAS 9.4
Thank you!
There are two methods to do what you want:
1) if formated value = original value then clear it to blank.
the syntax for numeric origin and for alphanumeric origin is different.
2) cange the format to include other option, for example
proc format;
value test
1 = 'A'
2 = 'B'
3 = 'C'
other = ' '
; run;
in a case you have a format and you can't ot not allowed to change it - add the other option,
then you can create a variation in work (temporary) catalog:
proc format lib=mylib cntlout=cntl;
select myfmt;
run;
data cntl;
set cntl end=eof;
output;
if eof then do;
OLH = 'O'; /* O=other, L=low, H=high */
label = ' ';
output;
end;
run;
proc format lib=work cntlin=cntl; run;
options fmtsearch = (work mylib);
There are two methods to do what you want:
1) if formated value = original value then clear it to blank.
the syntax for numeric origin and for alphanumeric origin is different.
2) cange the format to include other option, for example
proc format;
value test
1 = 'A'
2 = 'B'
3 = 'C'
other = ' '
; run;
in a case you have a format and you can't ot not allowed to change it - add the other option,
then you can create a variation in work (temporary) catalog:
proc format lib=mylib cntlout=cntl;
select myfmt;
run;
data cntl;
set cntl end=eof;
output;
if eof then do;
OLH = 'O'; /* O=other, L=low, H=high */
label = ' ';
output;
end;
run;
proc format lib=work cntlin=cntl; run;
options fmtsearch = (work mylib);
Thank you for your advice, Shmuel!
I'm currently using the first method you suggested (an if statement) as it seems to be the fastest/cleanest way so far. I was wondering if there is a "built-in" option to use for returning a user-specified negative reply instead of the automatic one. It does not seem like there is, so thank you anyway.
Have you used or trird to use the option other in your format ?
Oh, my bad. I understand now. The other tag isn't just for the exact match of "other".
That should do it then. Thank you!
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.