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

I have a list, for example a, b, c. It can be put into a variable with values a, b, c.

I would like to make it into [a], [b], [c].

Strangely, catx('[ ', variable, ']') produces values "[] a", "[] b", "[]c". Why?

Any suggestions are welcome.

Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
SASKiwi
PROC Star

Try this:

data _null_;
  list = 'a, b, c';
  attrib list_new length = $ 200;
  do i = 1 to countw(list,',');
    list_new = catx(', ', list_new, cats('[', scan(list,i), ']'));
  end;
  put _all_;
run;

View solution in original post

8 REPLIES 8
Reeza
Super User
I think you want CATT not CATX().
X specifies a delimiter between each term, which is the first parameter.

Quentin
Super User

I'm a little confused. Is this in a DATA step with one variable?

 

So you have:

data have;
  list='a, b, c';
run;

?

 

Or is this a macro setting where you have:

%let list= a, b, c ;

?

 

In either way, SAS doesn't know it's a list, but you can for example iterate over the items to update the values.

 

Or you can approach it as text replacement, e.g.:

data have;
  list='a, b, c';
  want=cats('[',transtrn(list,', ','], ['),']') ;
  put list= ;
  put want= ;
run;
ballardw
Super User

@pink_poodle wrote:

I have a list, for example a, b, c. It can be put into a variable with values a, b, c.

I would like to make it into [a], [b], [c].

Strangely, catx('[ ', variable, ']') produces values "[] a", "[] b", "[]c". Why?

Any suggestions are welcome.

Thank you!


You would have to show a complete data step and possibly some data as I don't actually your believe your output claim given the little bit of sort of code that you show.

77   data example;
78      variable='a';
79      newvar = catx('[',variable,']');
80      put newvar=;
81   run;

newvar=a[]

so the [] would appear after the value of variable with a [ preceding the next value

pink_poodle
Barite | Level 11

Yes, thank you! I want the output to be something like  ‘[a]’. Instead, it is ‘a[ ]’.

ballardw
Super User

Data.

Example data.

 

Basic for one value would be something like

   newvar = cats('[',variable,']');

but you keep saying "list". So we need to see what you currently have. So far it is not clear if you have one variable, 3 or more variables, or whatever. Plus we can't tell if there are multiple values in a single variable that need to be torn apart and reassembled.

SASKiwi
PROC Star

Try this:

data _null_;
  list = 'a, b, c';
  attrib list_new length = $ 200;
  do i = 1 to countw(list,',');
    list_new = catx(', ', list_new, cats('[', scan(list,i), ']'));
  end;
  put _all_;
run;
Tom
Super User Tom
Super User

How do you have the list?  Is it in a dataset?

data have;
  input variable $ ;
cards;
a
b
c
;

How do you want the result?  Do you want it in a dataset?

data want;
  set have end=eof;
  length list $100 ;
  list=catx(',',list,cats('[',variable,']'));
  retain list;
  if eof;
  keep list;
run;

And why did you ask about parentheses () if you want to use square brackets [] instead?

pink_poodle
Barite | Level 11
Thank you, all! This worked!
Best wishes.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 8 replies
  • 2350 views
  • 3 likes
  • 6 in conversation