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 -
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.