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
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;
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;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.