BookmarkSubscribeRSS Feed
xyxu
Quartz | Level 8

My string variable contains apostrophes and foreign characters in a few observations. For example,

 

data have;
	input var $30.;
	datalines;
Let's go for dinner
pi�ata dance collective
Another 인천
The 4th line
;
run;
I want to remove the apostrophe (') in the first observation, and delete the second and third observations because they contain characters that are neither alphabet nor number. Is there a good way to do this in SAS?

 

4 REPLIES 4
LinusH
Tourmaline | Level 20

You can use the COMPRESS function to remove certain characters.

Then there's a lot of function to identify different type of characters in a string:

https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/n01f5qrjoh9h4hn1olbdpb5pr2td.h...

I would try a combination of the functions starting with NOT...

Data never sleeps
Patrick
Opal | Level 21

The weird characters you're getting are likely due to using a single byte encoded editor or environment for multibyte characters (i.e. UTF-8).

Below should satisfy what you're asking for.

data want;
  set have;
  var=compress(var,"'");
  if findc(var,' ','kfnp') then delete;
run;
xyxu
Quartz | Level 8
Thanks! Do you have any suggestions for improving the editor's encoding issues? I used SAS 9.4 (English). If I add encoding='utf-8' into the import code, I got

ERROR: Invalid string.
FATAL: Unrecoverable I/O error detected in the execution of the DATA step program.
Aborted during the EXECUTION phase.
Ksharp
Super User
data want;
 set have;
 var=compress(var,"'");
 if prxmatch('/[^a-z\d\s]/i',var) then delete;
run;

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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