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 -

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 521 views
  • 2 likes
  • 3 in conversation