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')
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
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.
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
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
Bart solution worked best for me because it removed the space too. thank you so much
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.
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 秒
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!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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: