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

Hi there, 

 

I have a character variable that contains values that only include characters, values that only contain numeric, and other values that contain a combination of both numeric and alpha characters. I have included a small list of potential variable values below. 

 

1811

1826

1st airport 

1000 islands

1111

: Heathrow

9928

 : Seattle 

AC2277

 

I am trying to recode values that only contain numerics as "NA" (i.e obs 1, 2, 5, 7), and I was wondering if anyone had any idea on how this can be done? The dataset I am working with is quite large (observations in the millions), so manually re-coding this variable based on the proc freq outputs can be quite exhaustive. 

 

Any tips you would have to resolve this issue, would be very much appreciated!

1 ACCEPTED SOLUTION

Accepted Solutions
Quentin
Super User

There is a NOTDIGIT function that could be used.  It returns 0 if a string is all digits.

 

data want;
  input x $ 50.;

  length y $50 ;
  if notdigit(trim(x)) = 0 then y='NA' ;
  else y=x ;

cards;
1811
1826
1st airport 
1000 islands
1111
: Heathrow
9928
 : Seattle 
AC2277

;
run;

 

The Boston Area SAS Users Group (BASUG) is hosting our in person SAS Blowout on Oct 18!
This full-day event in Cambridge, Mass features four presenters from SAS, presenting on a range of SAS 9 programming topics. Pre-registration by Oct 15 is required.
Full details and registration info at https://www.basug.org/events.

View solution in original post

4 REPLIES 4
svh
Lapis Lazuli | Level 10 svh
Lapis Lazuli | Level 10

Sometimes, the COMPRESS() function can be used to handle situations like this, if you use the third argument. (There are lots of ways you can define what kinds of characters to keep or drop, so COMPRESS() is really quite handy. Especially for people like me who cannot use regex to save my life.  Note that there is nothing in the second argument of the "COMPRESS" function because I am not listing out the digits that I'd like to keep.

data have;
input x $ 50.;
cards;
1811
1826
1st airport 
1000 islands
1111
: Heathrow
9928
 : Seattle 
AC2277
;
run;
proc print;
run;
data have2;
  set have;
  put y $char50.;
  x1 = compress(x, , 'kd');
  if x1 EQ x then y = 'NA';
  else y= x;
  run;

 

Quentin
Super User

There is a NOTDIGIT function that could be used.  It returns 0 if a string is all digits.

 

data want;
  input x $ 50.;

  length y $50 ;
  if notdigit(trim(x)) = 0 then y='NA' ;
  else y=x ;

cards;
1811
1826
1st airport 
1000 islands
1111
: Heathrow
9928
 : Seattle 
AC2277

;
run;

 

The Boston Area SAS Users Group (BASUG) is hosting our in person SAS Blowout on Oct 18!
This full-day event in Cambridge, Mass features four presenters from SAS, presenting on a range of SAS 9 programming topics. Pre-registration by Oct 15 is required.
Full details and registration info at https://www.basug.org/events.
jnivi
Calcite | Level 5

Thank you so much!! This solution worked perfectly!

Astounding
PROC Star

Here are a few possibilities to consider, before deciding on a solution.  Would the values below:

  • stay as is
  • change to "NA"
  • doesn't matter, it won't happen
1 3 5      /* embedded blanks  */
3.1415     /* decimal point    */
    246    /* leading blanks   */
-789       /* negative sign    */
+987       /* positive sign    */
+ 246     /* combinations      */

If these are too easy, you might want to consider scientific notation such as  314159E-5

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 846 views
  • 1 like
  • 4 in conversation