BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Ronein
Meteorite | Level 14

Hello,

I have dynamic data set that can have different number of column .

Some of the columns in the data set have name with similar strcuture  Mon1,Mon2,Mon3  and so on.

As I said before number of columns with name Mon is not fixed.

I want to create a new column called "Indicator" which will get value 1 if there are at least 2 column with different values in column Mon1,Mon2,Mon3 and so on.

For example:

If there are 3 columns then the condition is :

IF Mon2 ne Mon1 OR Mon3 ne Mon2 then Indicator=1;else Indicator=0;

My question:

What is the way to create dynamic code that calculate column "Indicator" based on any number of columns Mon1,Mon2,Mon3....

Please note that the values in columns Mon1,Mon2,Mon3 are chars  with long string

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

When the number of columns can vary, but with relatively consistent naming conventions, then use ARRAY statement. This code is UNTESTED as you have not provided data.

 

data want;
    set have;
    array mon mon:;
    indicator=0;
    do i=2 to dim(mon);
        if mon(1)^=mon(i) then do;
            indicator=1;
            leave;
       end;
    end;
run;

 

--
Paige Miller

View solution in original post

1 REPLY 1
PaigeMiller
Diamond | Level 26

When the number of columns can vary, but with relatively consistent naming conventions, then use ARRAY statement. This code is UNTESTED as you have not provided data.

 

data want;
    set have;
    array mon mon:;
    indicator=0;
    do i=2 to dim(mon);
        if mon(1)^=mon(i) then do;
            indicator=1;
            leave;
       end;
    end;
run;

 

--
Paige Miller

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 1 reply
  • 753 views
  • 1 like
  • 2 in conversation