BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
bhanuprakash
Obsidian | Level 7

Hi Team,

 

I am able to replace special characters with empty spaces in the text2 argument. However I am not able to achieve the same in the text3 argument.  Can you please let us know how to use other functions/ variable names in the Perl regular expression?

 

data a;
input text $80.;
datalines;
The phone numberØ for Ed is (801)443-9876Ø but not until tonight.
He can be reached Øat (910)998-8762 Øtomorrow for testing purposesØ.
;
run;

data b;
set a;
text2=prxchange("s/Ø//", -1, text);
test3 = prxchange("s/byte(216)//", -1, text);
test4= byte(216);
run;

 

Thanks & Regards, 

Bhanu Prakash Pala

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisNZ
Tourmaline | Level 20

Same thing.

Another way to code that:

 VAR2 = prxchange('s/[^\x21-\x7E]/ /', -1, VAR); 

or

 VAR2 = prxchange('s/[\x00-\x1F\x7F-\xFF]/ /',-1, VAR); 

 

 

View solution in original post

9 REPLIES 9
sbxkoenk
SAS Super FREQ

I only use regular expressions for the real complex stuff.

 

You can get very far with these 3 SAS functions :

  • TRANSLATE Function
  • TRANSTRN Function
  • TRANWRD Function

 

Thanks,

Koen

bhanuprakash
Obsidian | Level 7

Yes, I was aware that we can achieve using those functions. I wanted to use a regular expression to achieve the requirement.

 

Please suggest to me if there are any other ways in the regular expressions.  

ChrisNZ
Tourmaline | Level 20

If special character means anything outside of the lower ascii characters (33 to 126, i.e. ! to ~), this might help:

VAR2 = prxchange('s/[^!-~]/ /', -1, VAR);

Be careful if using UTF8 encoding.

bhanuprakash
Obsidian | Level 7

I wanted to replace blank if we have ASCII specifically characters (1-31,127-255) in the Text. I can’t type those characters manually.  

Our program should work in dynamic way.

ChrisNZ
Tourmaline | Level 20

Same thing.

Another way to code that:

 VAR2 = prxchange('s/[^\x21-\x7E]/ /', -1, VAR); 

or

 VAR2 = prxchange('s/[\x00-\x1F\x7F-\xFF]/ /',-1, VAR); 

 

 

bhanuprakash
Obsidian | Level 7
Thank you so much Chris.
This is exactly what I am expecting.
Where can I find the list to identify the HEX values(\x00-\x1F\x7F-\xFF) which you mentioned.
ChrisNZ
Tourmaline | Level 20

Any ASCII table will show the list of characters. Just search that.

Patrick
Opal | Level 21

@bhanuprakash Given that your RegEx actually removes characters without replacing them with a blank the compress() function might also do the job for you. 

To be considered for using compress():

1. compress() will perform better than RegEx which might be relevant in case you're dealing with bigger data volumes

2. The SAS RegEx functions can only get used with single byte encoded data. If your data is multibyte encoded then consider using function kcompress() with the appropriate modifiers.

data a;
  input text $80.;
  datalines;
The phone numberØ for Ed is (801)443-9876Ø but not until tonight.
He can be reached Øat (910)998-8762 Øtomorrow for testing purposesØ.
;
run;

data b;
  set a;
  text2=prxchange("s/Ø//", -1, text);
  text3=prxchange('s/[\x01-\x1B\x7F-\xFF]//', -1, text);
  text4=compress(text,' ','knp');
run;

proc print data=b;
run;

And to find the hex values for ASCII values just Google for ASCII Table and you'll find what you need.

sbxkoenk
SAS Super FREQ

Post-Factum, but may still be useful to you !

 

Transcoding: Understand, Troubleshoot, and Resolve a Most Mysterious SAS® Error
Posted 03-15-2021 09:43 AM | by imujulie
https://communities.sas.com/t5/SAS-Global-Forum-Proceedings/Transcoding-Understand-Troubleshoot-and-...

 

Koen

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 9 replies
  • 2416 views
  • 2 likes
  • 4 in conversation