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;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 4 replies
  • 840 views
  • 2 likes
  • 4 in conversation