Is there a function or setting to make a user-defined format return blank if no match?

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

Is there a function or setting to make a user-defined format return blank if no match?

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!


Accepted Solutions
Solution
‎01-24-2017 01:53 PM
Super User
Posts: 1,232

Re: Is there a function or setting to make a user-defined format return blank if no match?

[ Edited ]

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);
        

 

 

View solution in original post


All Replies
Solution
‎01-24-2017 01:53 PM
Super User
Posts: 1,232

Re: Is there a function or setting to make a user-defined format return blank if no match?

[ Edited ]

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);
        

 

 

New Contributor
Posts: 3

Re: Is there a function or setting to make a user-defined format return blank if no match?

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.

Super User
Posts: 1,232

Re: Is there a function or setting to make a user-defined format return blank if no match?

Have you used or trird to use the option other in your format ?

New Contributor
Posts: 3

Re: Is there a function or setting to make a user-defined format return blank if no match?

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!

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 170 views
  • 4 likes
  • 2 in conversation