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!

sas-innovate-2024.png

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.

 

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
  • 4 replies
  • 934 views
  • 1 like
  • 3 in conversation