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 find all the special characters? and split the special character in a new variable?

for example 

 

for JUICE∫app$le,

I expect to get 

old specialnew   
JUICE∫app$leJUICE app le
JUICE∫app$le$JUICE app le

data have ;
  infile datalines truncover;
  input name $100.;
  datalines;
JUICE∫app$le
juice <BR> apple 
juice, 'apple' 
juice{BODY} ap¶ple 
[BR]juice apple
<figure> "juice" LTD 
;
run;

 

(Special characters are characters that are not part of the following set of letters, digits and punctuation characters: A-Z; 0-9; “-“; “+”; “’”; “””; “#”; “*”; “@”; “!”; “?”; “/”; “&”; “(“; “)”; “:”; “;”; “,”; “.”; “ “.)

 

Could you please give me some suggestions about this?

thanks in advance 

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20
data want;
   format old special new;
   set have;
   RegExID = prxparse('/[^A-Z|a-z|0-9|-|\+|’|”|#|\*|@|!|\?|\/|&|\(|\)|:|;|,|.]/');
   start=1;
   call prxnext(RegExID, start, length(old), old, pos, length);
   new=prxchange('s/[^A-Z|a-z|0-9|-|\+|’|”|#|\*|@|!|\?|\/|&|\(|\)|:|;|,|.]/ /', -1, old);
      do while (pos > 0);
         special = substr(old, pos, length);
         output;
         call prxnext(RegExID, start, length(old), old, pos, length);
      end;
   keep old special new;
run;

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26

You could use the FINDC function, with modifiers "A", "K", "D", and the character list would be the punctuation you don't want to find as shown in your message.

 

https://documentation.sas.com/?cdcId=pgmmvacdc&cdcVersion=9.4&docsetId=lefunctionsref&docsetTarget=n...

--
Paige Miller
andreas_lds
Jade | Level 19

You could use compress function twice to get both strings. Try:

data want;
    set have;
    
    length special new $ 100 specialChars $ 20;
    retain specialChars;
    drop specialChars;
    
    if _N_ = 1 then do;
        specialChars = cat('-+#*@!?/&():;,. "', "'");
    end;
    
    special = compress(name, specialChars, 'adik');
    new = compress(name, specialChars, 'adi');
run;
PeterClemmensen
Tourmaline | Level 20
data want;
   format old special new;
   set have;
   RegExID = prxparse('/[^A-Z|a-z|0-9|-|\+|’|”|#|\*|@|!|\?|\/|&|\(|\)|:|;|,|.]/');
   start=1;
   call prxnext(RegExID, start, length(old), old, pos, length);
   new=prxchange('s/[^A-Z|a-z|0-9|-|\+|’|”|#|\*|@|!|\?|\/|&|\(|\)|:|;|,|.]/ /', -1, old);
      do while (pos > 0);
         special = substr(old, pos, length);
         output;
         call prxnext(RegExID, start, length(old), old, pos, length);
      end;
   keep old special new;
run;

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