Hello!
I have a (macro) variable with a value in the form
"AB";"AG";"FZ","IE"
of variable length.
I would like to create a new variable with all the characters from my (macro) variable and blanks in between, i.e.
AB AG FZ IE
I tried the SCAN function in a do loop in the DATA step but I got an error because of the ;s in the value of the (macro) variable.
How can I get it done?
Thx,
Stefan
Hi ,
Try this..Hope it helps...
%let x=%str("AB";"AG";"FZ","IE");
%let xx1 =%sysfunc(compbl(%sysfunc(translate(&x,' ','" ; ,'))));
Thanks,
Shiva
Hi ,
Try this..Hope it helps...
%let x=%str("AB";"AG";"FZ","IE");
%let xx1 =%sysfunc(compbl(%sysfunc(translate(&x,' ','" ; ,'))));
Thanks,
Shiva
Thx, Shiva! What works! What does it do?
Cheers, Stefan
In this solution the TRANSLATE function is changing the special characters to blanks, and then the COMPBL function converts multiple blanks to a single blank. There are other functions that could have also been used COMPRESS and DEQUOTE among them.
Also this soution uses single quotes to delineate the text strings in the TRANSLATE function. When using macro language elements this technique can have unintended consequences. Macro language quoting functions such as %STR and %BQUOTE, shown below, do not have these issues.
%let xx2 =%sysfunc(compbl(%sysfunc(translate(&x,%str( ),%bquote(" ; ,)))));
I once had got a number of consulting hours to debug a program, ultimately the problem was found in the following code fragment
%scan(&name, &i, ' ')
which works fine unless the &name contains an apostrophe such as O'Conner
Thx!
⏰
Today is the last day to save with the early bird rate! Register today for just $695 - $100 off the standard rate.
Plus, pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
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.