BookmarkSubscribeRSS Feed
lydiawawa
Lapis Lazuli | Level 10

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.

2 REPLIES 2
andreas_lds
Jade | Level 19

Have a closer look at the parameters of scan: i think a comma is missing right after calling vname.

Oligolas
Barite | Level 11

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 -

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
  • 2 replies
  • 1041 views
  • 2 likes
  • 3 in conversation