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!
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;
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;
@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
Yes, thank you! I want the output to be something like ‘[a]’. Instead, it is ‘a[ ]’.
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.
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;
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?
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!
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.
Ready to level-up your skills? Choose your own adventure.