BookmarkSubscribeRSS Feed
Babloo
Rhodochrosite | Level 12

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

data Have:

+1234

123 4

12-3

1234

data want:

1234

8 REPLIES 8
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10

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.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Hi,

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

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

Babloo
Rhodochrosite | Level 12

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?

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

Astounding
PROC Star

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;

slchen
Lapis Lazuli | Level 10

data Have;

input num $6.;

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

cards;

+1234

213 0

782-3

1239

;

run;

Ksharp
Super User

data Have;

input num $20.;

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

cards;

+1234

213 0

782-3

1239

;

run;

Ksharp
Super User

data Have;

input num $20.;

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

cards;

+1234

213 0

782-3

1239

;

run;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 8 replies
  • 1897 views
  • 7 likes
  • 6 in conversation