Hi,
I'm trying to create value based on characters in variable names after certain string. The following is the code that generates blank when combined catx with scan:
data master1_color;
length color_new $50;
set master1;
array r ABC_WHITE ABC_BLACK ABC_YELLOW ABC_RED ACS_OTHER;
do _i = 1 to dim(r);
if r[_i] = '1' then color_new = catx('/',color_new,scan(vname(r(_i))-1,'_'));
end;
drop i;
run; I'm hoping to generate values such as:
Color_new:
WHITE/BLACK
BLACK
BLACK/RED
etc.
However, the catx outputs blanks instead of concatenated strings. It will work if I remove scan in catx.
Have a closer look at the parameters of scan: i think a comma is missing right after calling vname.
Hi,
yep a comma is missing and you could light your code up
DATA master1;
input ABC_WHITE $ ABC_BLACK $ ABC_YELLOW $ ABC_RED $ ACS_OTHER $;
datalines;
0 0 0 0 0
1 0 0 0 0
0 1 0 0 1
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
1 1 1 1 1
;
run;
DATA master1_color;
LENGTH color_new $50;
SET master1;
ARRAY r _CHARACTER_;
do over r;
if r = '1' then color_new = catx('/',color_new,scan(vname(r),-1,'_'));
end;
RUN;
- Cheers -
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.