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

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!

1 ACCEPTED SOLUTION

Accepted Solutions
Shmuel
Garnet | Level 18

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

4 REPLIES 4
Shmuel
Garnet | Level 18

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

 

 

azhou_uhg
Calcite | Level 5

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.

Shmuel
Garnet | Level 18

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

azhou_uhg
Calcite | Level 5

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!

sas-innovate-2024.png

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.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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