BookmarkSubscribeRSS Feed
carl_miles
Fluorite | Level 6

Hi Folks,

My given data set 

data mixed_data;
  length mixed_variable $20.;
  
  input mixed_variable $20.;
  datalines;
aB#5dH0p2Lq8R!xJ7sE9
3kFy@6u1oPwQvZr4Xb2
s9TgY0o2I!qLw5mH8xN7
dC4pL!aB9eR2o7Xn1yH6
6fWvGm9@jP3tNqLr1iH2
bS5lE7dR0oA1!xQ8zC3
Kp2Jr!L6yFw7XsNv9H0e
4uO9rH#tWq1E7sY2cL6
xZ3yF2!iQ8vS0l1Hr7nG
5pA1!mQ9o2W8rHl3vC6
;
run;

proc print data=mixed_data;
run;

i want keep only character(alphabets) and numbers only and remove other characters and including spaces

I used function Compress(mixed_variable, ,sakkd);
however s - to remove spaces
ak - to keep alphabets 
kd - to keep numbers 
are there any other way?

I am not sure of sakkd function my output if fine but i want make sure its does a right job I want other ways and validation as well 

3 REPLIES 3
PaigeMiller
Diamond | Level 26

I would think the final argument to COMPRESS would be 'adk'

--
Paige Miller
A_Kh
Lapis Lazuli | Level 10
  New_Var=compress(mixed_variable, '', 'p');

To remove any punctuation or special characters

Tom
Super User Tom
Super User

If space are important then include some spaces in the test data.

data mixed_data;
  input mixed_variable $20.;
  datalines;
aB#5dH0p2Lq8R!xJ7sE9
3kFy @6u1oPwQvZr4Xb2
s9TgY0o2I!qLw5mH8xN7
dC4pL!aB9eR2o7Xn1yH6
6fWvGm9@jP3tNqLr1iH2
bS5lE 7dR0oA1!xQ8zC3
Kp2Jr!L6yFw7XsNv9H0e
4uO9rH# tWq1E7sY2cL6
xZ3yF2!iQ8vS0l1Hr7nG
5pA1!m    Hl3 v   C6
;

It sounds like you are saying you want to eliminate everything except digits letters and spaces.

data want;
  set mixed_data;
  want = compress(mixed_variable,' ','kad');
run;

Which will generate:

Obs       mixed_variable              want

  1    aB#5dH0p2Lq8R!xJ7sE9    aB5dH0p2Lq8RxJ7sE9
  2    3kFy @6u1oPwQvZr4Xb2    3kFy 6u1oPwQvZr4Xb2
  3    s9TgY0o2I!qLw5mH8xN7    s9TgY0o2IqLw5mH8xN7
  4    dC4pL!aB9eR2o7Xn1yH6    dC4pLaB9eR2o7Xn1yH6
  5    6fWvGm9@jP3tNqLr1iH2    6fWvGm9jP3tNqLr1iH2
  6    bS5lE 7dR0oA1!xQ8zC3    bS5lE 7dR0oA1xQ8zC3
  7    Kp2Jr!L6yFw7XsNv9H0e    Kp2JrL6yFw7XsNv9H0e
  8    4uO9rH# tWq1E7sY2cL6    4uO9rH tWq1E7sY2cL6
  9    xZ3yF2!iQ8vS0l1Hr7nG    xZ3yF2iQ8vS0l1Hr7nG
 10    5pA1!m    Hl3 v   C6    5pA1m    Hl3 v   C6

If you also want to eliminate spaces then do not put anything at all for the second argument.

  want = compress(mixed_variable,,'kad');

Or put some character you DO want to keep in the second argument.

data want;
  set mixed_data;
  want = compress(mixed_variable,'A','kad');
run;

 

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
  • 3 replies
  • 708 views
  • 1 like
  • 4 in conversation