DATA Step, Macro, Functions and more

SAS function to remove records which contains special characters

Reply
Super Contributor
Posts: 426

SAS function to remove records which contains special characters

I request someone guide me to remove the records which contains special characters.

data Have:

+1234

123 4

12-3

1234

data want:

1234

Super Contributor
Super Contributor
Posts: 3,174

Re: SAS function to remove records which contains special characters

Look at using a DATA step to INPUT your data-records, then test using one of the appropriate ANY-prefix SAS functions found in the SAS Language Elements / CALL function documentation.  A tip is that you can use _INFILE_ as the argument passed to your function call, although it is unclear if you want to create an output file (non-SAS) or a SAS data library member based on your input -- that would determine whether or not a FILE statement is warranted as opposed to a SAS member on your DATA statement, instead of DATA _NULL_;

Scott Barry
SBBWorks, Inc.

Super User
Super User
Posts: 7,392

Re: SAS function to remove records which contains special characters

Hi,

Compress.  If you want on a single result from them, then sort nodupkey.

new_variable=compress(old_variable,,'kd');

Super Contributor
Posts: 426

Re: SAS function to remove records which contains special characters

My real data looks like,

data Have:

+1234

213 0

782-3

1239

data want:

1239

In this case how to dealt with compress function? Any other functions to look for?

Super User
Super User
Posts: 7,392

Re: SAS function to remove records which contains special characters

data want;

     set have;

     if length(strip(compress(the_string,,'d'))) = 0 then output;

run;

I.e. if I remove all numbers, and length is zero, there is nothing other than numbers.

Super User
Posts: 5,080

Re: SAS function to remove records which contains special characters

That's a good approach, but it needs a little tweaking on two counts.  First, STRIP isn't needed.  Leading and trailing blanks don't need to be removed if you only want to detect whether any characters remain.  Second, LENGTH has a quirk:  it never returns zero.  The minimum value returned is 1.  The easiest way to overcome this is to switch to LENGTHN:

if lengthn(compress(the_string,,'d'))=0 then output;

Super Contributor
Posts: 275

Re: SAS function to remove records which contains special characters

data Have;

input num $6.;

if prxmatch('/^\d{4}/o',strip(num))^=0 then output;

cards;

+1234

213 0

782-3

1239

;

run;

Super User
Posts: 9,671

Re: SAS function to remove records which contains special characters

data Have;

input num $20.;

if not prxmatch('/\D/',strip(num)) then output;

cards;

+1234

213 0

782-3

1239

;

run;

Super User
Posts: 9,671

Re: SAS function to remove records which contains special characters

data Have;

input num $20.;

if not notdigit(strip(num)) then output;

cards;

+1234

213 0

782-3

1239

;

run;

Ask a Question
Discussion stats
  • 8 replies
  • 387 views
  • 7 likes
  • 6 in conversation