BookmarkSubscribeRSS Feed
Emma_at_SAS
Lapis Lazuli | Level 10

I have a character variable that contains alphabets, numbers, some symbols, and both ' and " and also some strange characters (characters that are not on any keyboard). I want to create a new variable with only the characters I want. Because I am using " before and after my list, I cannot write " to keep it in my observations for the variable var. How may I include "?

Thanks!

 

data want;
set have;
new_var=compress(var,"ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890$%.-':|&[]{}*!@#()!/+,");
run;
12 REPLIES 12
Tom
Super User Tom
Super User

To include the character used to quote a string literal just double it up in the string content. Example:

word = 'Don''t';

So if you are using " on the outside to include it as one of the characters just add "" to the list.

compress(var,"ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890$%.-""':|&[]{}*!@#()!/+,")

 

ErikLund_Jensen
Rhodochrosite | Level 12

Hi @Emma_at_SAS 

 

It might be easier to use the "k" modifier. It reverses the compress, so only the specifies characters are kept indtead of dropped.

 

Drop all printable and non-printable chars except "ABC":   

compress(str,'ABC','k');

 

Drop all printable and non-printable chars except digits, english chars and underscore. There are no characters specified, but the mentioned chars are added with the 'n' modifier. More examples in the online documentation: 

compress(str,,'nk');

 

 

Emma_at_SAS
Lapis Lazuli | Level 10

Thank you, @Tom and @ErikLund_Jensen. A combination of your suggestions solved my problem

I used the double "" and,"k" modifier and that gives me exactly what I want.

Thanks!

Emma_at_SAS
Lapis Lazuli | Level 10

May someone please help me to mark this conversation as solved? I am not sure because it was solved based on two suggestions.

Thanks

novinosrin
Tourmaline | Level 20

How about just-

 

compress(var,,'p');
Emma_at_SAS
Lapis Lazuli | Level 10
Thank you, novinosrin. What does this combination do? Thanks!
Amir
PROC Star

Hi @Emma_at_SAS,

 

From the documentation of the scan() function, when the modifier argument is set to "p", as in @novinosrin's post, punctuation is added to the list of characters that are to be removed from the text:

 

p or P adds punctuation marks to the list of characters.

 

 

Kind regards,

Amir.

Ksharp
Super User
new_var=compress(var, , 'kg');
Emma_at_SAS
Lapis Lazuli | Level 10

 

data test;
input var$ ;
datalines;
oÔÇÖ\[}s 
dsd_'"sf 
;
run;

 

Obs var
1 oÔÇÖ\[}s
2 dsd_'"sf

 

data want;
set test;
new_var=compress(var,"ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890$%.-'"":|&[]{}*!@#()!/+, ",'kg');
run;

proc print data=want; run;
The SAS System

Obs var new_var
1 oÔÇÖ\[}s oÔÇÖ\[}s
2 dsd_'"sf dsd_'"sf

 Thank you @Ksharp for your suggestion. I already have a solution but I thought this might be easier to just remove the "graphic characters". Beacuse I want to keep everything except the translation of emojis that appear in my SAS dataset. May you please help me with an example? Thanks

 

Ksharp
Super User
You could list all the characters you want keep as you did with 'k'.

new_var=compress(var,"ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890$%.-'"":|&[]{}*!@#()!/+, ", 'k'
);
Emma_at_SAS
Lapis Lazuli | Level 10
Thank you, Ksharp. This is what I was using the 'k' modifier. I was hoping to be able to use the 'g' modifier to only exclude the graphic characters. Maybe at some point, I learn to work efficiently with the modifiers. Thanks!
Ksharp
Super User
exclude 'g' --> could use 'g' instead of 'k'.
new_var=compress(var,"ABC " ,'g');

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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
  • 12 replies
  • 860 views
  • 6 likes
  • 6 in conversation