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

Hi Everyone,

 

I have got the below data step:

data nn;
input email :$20.;
list = catx(';','Juke@hotmail', 'Frida@gmail');
if findc(trim(email),List) then Cleansed_List = tranwrd(List,trim(email),'');
cards;
Juke@hotmail
Frida@gmail
Killian@yahoo.com
;

It creates Cleansed_List with a semi colon as delimiter in the wrong places e.g preceding certain values

 

 

What I want to create is Cleansed list, with no semi-colon delimiter at the start and end of the resulting values.

 

Can anyone help please?

1 ACCEPTED SOLUTION

Accepted Solutions
data_null__
Jade | Level 19

The TRANSTRN function is what you seek.

 

data nn;
input email :$20.;
list = catx(';','Juke@hotmail', 'Frida@gmail');
if findc(trim(email),List) then Cleansed_List = tranSTRN(List,trim(email),trimn(' '));
x = cleansed_list;
cards;
Juke@hotmail
Frida@gmail
Killian@yahoo.com
;;;;
   run;

View solution in original post

7 REPLIES 7
Astounding
PROC Star

Easiest would be to eliminate all the semicolons:

 

list = catx(' ','Juke@hotmail', 'Frida@gmail');

 

The rest of the program should still work just fine. 

Reeza
Super User
Show what your input data looks like and what you expect as output please.
data_null__
Jade | Level 19

The TRANSTRN function is what you seek.

 

data nn;
input email :$20.;
list = catx(';','Juke@hotmail', 'Frida@gmail');
if findc(trim(email),List) then Cleansed_List = tranSTRN(List,trim(email),trimn(' '));
x = cleansed_list;
cards;
Juke@hotmail
Frida@gmail
Killian@yahoo.com
;;;;
   run;
FreelanceReinh
Jade | Level 19

Hi @frupaul,

 

FINDC is most likely not what you want: It searches for characters, not substrings of length >1. FIND would be more appropriate, but with reversed order of arguments. TRANSTRN would be preferable to TRANWRD as has been mentioned already. However, it would still leave double semicolons if an email address in the middle of a longer list was removed or the leading or trailing semicolons you have observed. PRXCHANGE should work, but it's maybe simpler to just divide the list and put it together again after omitting the email address(es) to be deleted:

data nn;
length list Cleansed_List $80;
array _em[99] $20;
input email $20.;
list = catx(';', 'Juke@hotmail', 'Frida@gmail');
do _i=1 to countw(list, ';');
  _emi=scan(list, _i, ';');
  if _emi ne email then _em[_i]=_emi;
end;
Cleansed_List = catx(';', of _em[*]);
drop _:;
cards;
Juke@hotmail
Frida@gmail
Killian@yahoo.com
;

(Make sure to increase lengths and number of array elements if necessary.)

frupaul
Quartz | Level 8

Thanks for your solution. I think you might have over complicated it using the do loops and stuff. But thanks for providing a solution

PGStats
Opal | Level 21

@FreelanceReinh's solution might look too complicated, but it is the only one that really works. PRXCHANGE would also work well, but compiling a new pattern for every observation would be very expensive.

PG
Tom
Super User Tom
Super User

What are you actually trying to do? 

 

Also please look at the definitions of FINDC() and TRANWRD() as I don't think you are using either of them in an appropriate way for what I think your question might be.  FINDC() is for finding first location of any one of the characters in a list. TRANWRD() for change one string to another (as opposed to TRANSLATE() that changes individual characters), but does not really understand the concept of a word despite its name. 

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
  • 7 replies
  • 1342 views
  • 3 likes
  • 7 in conversation