SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Alexxxxxxx
Pyrite | Level 9

Dear all,

 

how can I remove all punctuation characters (except '&')? 

 

I using the following codes 

 


data a;
  infile datalines dlm=',' truncover;
informat HRM_L2_Step6 $1000. ;
input HRM_L2_Step6 ;
datalines;
!OBAC
1...
1... IPR 
1.CALL RESEA-RCH & DESIGN 
100% RECYCLED PANEL COMPAMY 
20/20 SPEECH 
24/7 TECHNOLOGIES 
2S-SOFISTIK/EJ/TED SISTEMS 
3 S'S 
3-D COMPOSITES
3-D COMPOSITS 
3COM CORPORATION (COMPUTERS COMMUNICATION COMPATIBILITY CORPORATION) 
3M COMPANY (MINNESOTA MINING AND MANUFACTURING COMPANY)
3M INNOVATIVE PROPERTIES COMPANY (MINNESOTA MINING AND MANUFACTURING INNOVATIVE PROPERTIES COMPANY)
3RD ANGLE (U.K.)
3RD ANGLE (U.K.) LTD., HIGHLEY
3RD ANGLE (U.K.) LTD., HIGHLEY, SHROPSHIRE
>>PYROTEX
@ROAD
;
run;

Data b;
Set a;
HRM_L2_Step6=tranwrd(HRM_L2_Step6,"'",  "");
HRM_L2_Step6=tranwrd(HRM_L2_Step6,";",  "");
HRM_L2_Step6=tranwrd(HRM_L2_Step6,"^",  "");
HRM_L2_Step6=tranwrd(HRM_L2_Step6,"<",  "");
HRM_L2_Step6=tranwrd(HRM_L2_Step6,".",  "");
HRM_L2_Step6=tranwrd(HRM_L2_Step6,"`",  "");
HRM_L2_Step6=tranwrd(HRM_L2_Step6,"_",  "");
HRM_L2_Step6=tranwrd(HRM_L2_Step6,">",  "");
HRM_L2_Step6=tranwrd(HRM_L2_Step6,"''", "");
HRM_L2_Step6=tranwrd(HRM_L2_Step6,"!",  "");
HRM_L2_Step6=tranwrd(HRM_L2_Step6,"+",  "");
HRM_L2_Step6=tranwrd(HRM_L2_Step6,"?",  "");
HRM_L2_Step6=tranwrd(HRM_L2_Step6,"(",  "");
HRM_L2_Step6=tranwrd(HRM_L2_Step6,"£",  "");
HRM_L2_Step6=tranwrd(HRM_L2_Step6,"{",  "");
HRM_L2_Step6=tranwrd(HRM_L2_Step6,"\",  "");
HRM_L2_Step6=tranwrd(HRM_L2_Step6,")",  "");
HRM_L2_Step6=tranwrd(HRM_L2_Step6,"$",  "");
HRM_L2_Step6=tranwrd(HRM_L2_Step6,"}",  "");
HRM_L2_Step6=tranwrd(HRM_L2_Step6,"|",  "");
HRM_L2_Step6=tranwrd(HRM_L2_Step6,",",  "");
HRM_L2_Step6=tranwrd(HRM_L2_Step6,"%",  "");
HRM_L2_Step6=tranwrd(HRM_L2_Step6,"[",  "");
HRM_L2_Step6=tranwrd(HRM_L2_Step6,"¶",  "");
HRM_L2_Step6=tranwrd(HRM_L2_Step6,"-",  "");
HRM_L2_Step6=tranwrd(HRM_L2_Step6,"*",  "");
HRM_L2_Step6=tranwrd(HRM_L2_Step6,"]",  "");
HRM_L2_Step6=tranwrd(HRM_L2_Step6,"/",  "");
HRM_L2_Step6=tranwrd(HRM_L2_Step6,"@",  "");
HRM_L2_Step6=tranwrd(HRM_L2_Step6,":",  "");
HRM_L2_Step6=tranwrd(HRM_L2_Step6,"~",  "");
HRM_L2_Step6=tranwrd(HRM_L2_Step6,"#",  "");
run;

however, for example, for '!OBAC', I get ' OBAC' rather than 'OBAC'.

How can I get 'OBAC'?

could you please give me some suggestions about this? thanks in advance.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

In general, you should be looking at COMPRESS (which removes characters), rather than TRANWRD (which translates them into something else).

 

For most of your request, you can fit it into a single statement.  For example:

 

HRM_L2_Step6 = compress(HRM_L2_Step6, ";'<.+?/()" );

 

When you look at the COMPRESS documentation, you may also notice that a third parameter lets you use particular letters to abbreviate a request that applies to groups of characters.  You may find an abbreviation within that third parameter that handles most of the characters you are interested in removing:

 

https://documentation.sas.com/?docsetId=lefunctionsref&docsetTarget=n0fcshr0ir3h73n1b845c4aq58hz.htm...

View solution in original post

2 REPLIES 2
Astounding
PROC Star

In general, you should be looking at COMPRESS (which removes characters), rather than TRANWRD (which translates them into something else).

 

For most of your request, you can fit it into a single statement.  For example:

 

HRM_L2_Step6 = compress(HRM_L2_Step6, ";'<.+?/()" );

 

When you look at the COMPRESS documentation, you may also notice that a third parameter lets you use particular letters to abbreviate a request that applies to groups of characters.  You may find an abbreviation within that third parameter that handles most of the characters you are interested in removing:

 

https://documentation.sas.com/?docsetId=lefunctionsref&docsetTarget=n0fcshr0ir3h73n1b845c4aq58hz.htm...

ballardw
Super User

For your consideration:

data example;
   x="ABC!@#$%^&*()_+=-,./?><;:[]{}\|DEF`";
   y=compress(x,,'P');
run;

I didn't go through any additional steps to add the double quote character to X but you get the general idea.

 

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 6570 views
  • 2 likes
  • 3 in conversation