BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
grnize
Calcite | Level 5

I have an email string with a list of email addresses. each email address is surrounded by single quotes. i want to write a program that will remove or add an email address with the quotes around it.

 

For example

'donald.duck@cartoon.com'

I can search the string for the email to remove using compress but I cannot remove the single quotes around it because other email's have the quotes.

Any suggestions on how to get rid of the quotes once the email is gone or both at once?

 

before code 

example string: ('donald.duck@cartoon.com' 'becky.green@grass.com' 'mark.tomas@lbschool.net')

after process 

example string: ('' 'becky.green@grass.com' 'mark.tomas@lbschool.net')

 

1 ACCEPTED SOLUTION

Accepted Solutions
yabwon
Onyx | Level 15

Try like this, I assumed the list of emails is provides as a macro variable,

%let list=('donald.duck@cartoon.com' 'becky.green@grass.com' 'mark.tomas@lbschool.net');



data _null_;
  stringToDrop='donald.duck@cartoon.com';

  length example ex $ 32767;
  example=symget('list');
  put "before:" @10 example;
  do i=1 to countw(example, "( )");
    part=dequote(scan(example, i, "( )"));
    if part ne stringToDrop then
      ex=catx(" ", ex, quote(strip(part), "'") );
  end;

  example="(" !! strip(ex) !! ")";
  put "after:" @10 example;
run;

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



View solution in original post

6 REPLIES 6
Tom
Super User Tom
Super User

I cannot tell from your description what your dataset looks like. Or for that matter if you even have a dataset.

Post some example datasets.  Input and Output.  Please post them as code that can be run to re-create them.  It does not have to be real data or very much data.  Just something that makes it clear what you are trying to do.

 

And I cannot figure out how the COMPRESS() could be used to do anything that you asked about.  If you have tried something the show that also.  If you get error messages show the SAS log for the step with errors. If it just doesn't do what you want explain what is wrong with the output.

yabwon
Onyx | Level 15

Try like this, I assumed the list of emails is provides as a macro variable,

%let list=('donald.duck@cartoon.com' 'becky.green@grass.com' 'mark.tomas@lbschool.net');



data _null_;
  stringToDrop='donald.duck@cartoon.com';

  length example ex $ 32767;
  example=symget('list');
  put "before:" @10 example;
  do i=1 to countw(example, "( )");
    part=dequote(scan(example, i, "( )"));
    if part ne stringToDrop then
      ex=catx(" ", ex, quote(strip(part), "'") );
  end;

  example="(" !! strip(ex) !! ")";
  put "after:" @10 example;
run;

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



yabwon
Onyx | Level 15

An alternative with regexp:

%let list=('donald.duck@cartoon.com' 'becky.green@grass.com' 'mark.tomas@lbschool.net');
%let str='donald.duck@cartoon.com';


data _null_;
  length stringToDrop $ 256;
  stringToDrop=symget('str');

  /* mask special characters to get stringToDrop="\'donald\.duck\@cartoon\.com\'" */
  do s="'",'"',"@",'.';
    s2='\'!!s;
    stringToDrop=TRANWRD(stringToDrop, s, s2);
  end;
  put stringToDrop=;
  

  length example $ 32767;
  example=symget('list');
  put "before:" @10 example;
 
  example=PRXCHANGE('s/' !! strip(stringToDrop)!! '//', -1, example);
  put "after:" @10 example;
run;

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



grnize
Calcite | Level 5

Bart solution worked best for me because it removed the space too. thank you so much

Quentin
Super User

You can use TRANSTRN(), e.g.:

 

1    data want ;
2      string="('donald.duck@cartoon.com' 'becky.green@grass.com' 'mark.tomas@lbschool.net')" ;
3      string2=transtrn(string,"'donald.duck@cartoon.com'",trimn("")) ;
4      put string2= ;
5    run ;

string2=( 'becky.green@grass.com' 'mark.tomas@lbschool.net')
NOTE: The data set WORK.WANT has 1 observations and 2 variables.

This doesn't get rid of the extra blank left behind.

 

 

BASUG is hosting free webinars ! Check out recordings of our past webinars: https://www.basug.org/videos. Save the date for our in person SAS Blowout on Oct 18 in Cambridge, MA. Registration opens in September.
Ksharp
Super User
data _null_;
have="('donald.duck@cartoon.com' 'becky.green@grass.com' 'mark.tomas@lbschool.net')";
want=prxchange("s/'[^']+'//",1,have);

put have= / want=;
run;
73   data _null_;
74   have="('donald.duck@cartoon.com' 'becky.green@grass.com' 'mark.tomas@lbschool.net')";
75   want=prxchange("s/'[^']+'//",1,have);
76
77   put have= / want=;
78   run;

have=('donald.duck@cartoon.com' 'becky.green@grass.com' 'mark.tomas@lbschool.net')
want=( 'becky.green@grass.com' 'mark.tomas@lbschool.net')
NOTE: “DATA 语句”所用时间(总处理时间):
      实际时间          0.03 秒
      CPU 时间          0.00 秒

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
  • 6 replies
  • 780 views
  • 1 like
  • 5 in conversation