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

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

1 ACCEPTED SOLUTION

Accepted Solutions
shivas
Pyrite | Level 9

Hi ,

Try this..Hope it helps...

%let x=%str("AB";"AG";"FZ","IE");

%let xx1 =%sysfunc(compbl(%sysfunc(translate(&x,' ','" ; ,'))));

Thanks,

Shiva

View solution in original post

4 REPLIES 4
shivas
Pyrite | Level 9

Hi ,

Try this..Hope it helps...

%let x=%str("AB";"AG";"FZ","IE");

%let xx1 =%sysfunc(compbl(%sysfunc(translate(&x,' ','" ; ,'))));

Thanks,

Shiva

sfmeier
Obsidian | Level 7

Thx, Shiva! What works! What does it do?

Cheers, Stefan

ArtC
Rhodochrosite | Level 12

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

sfmeier
Obsidian | Level 7

Thx!

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1623 views
  • 1 like
  • 3 in conversation