BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
panda
Quartz | Level 8

My data looks like this:

data dat;
   input id ind01 ind02 ind03 ind04 ind05 ind06;
   datalines;
   1 0 0 0 0 4 0
   2 0 0 1 0 3 0 
   3 4 4 4 4 4 4
   4 1 2 3 2 2 2
   5 1 3 0 0 2 0 
   6 1 2 3 4 2 2
   ;
run;

I want to create a variable to stratify the rows into three groups: 

group A: have either 0 or 4 for all variables ind01-ind06; 

group B: have any of 1 or 2 or 3, for all variables in01-ind06;

group C: change from (0 or 4) to (1 or 2 or 3) (or vice versa) for all variables ind01-ind06;

 

The ideal dataset looks like this:

data want;
   input id ind01 ind02 ind03 ind04 ind05 ind06 type $;
   datalines;
   1 0 0 0 0 4 0 A
   2 0 0 1 0 3 0 C
   3 4 4 4 4 4 4 A
   4 1 2 3 2 2 2 B
   5 1 3 0 0 2 0 C
   6 1 2 3 4 2 2 C
   ;
run;

I am not sure how to create the variable since it needs tracking changes across multiple columns. Any suggestions? Thanks!!

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
data want;
input id ind01 ind02 ind03 ind04 ind05 ind06;
array ind ind01-ind06;
zero=0;
four=0;
do i=1 to dim(ind);
zero=zero+(ind(i)=0);
four=four+(ind(i)=4);
end;
if zero+four=6 then group='A';
else if zero+four=0 then group='B';
else group='C';
drop i;
datalines;
1 0 0 0 0 4 0
2 0 0 1 0 3 0 
3 4 4 4 4 4 4
4 1 2 3 2 2 2
5 1 3 0 0 2 0
6 1 2 3 4 2 2
;
run;
--
Paige Miller

View solution in original post

2 REPLIES 2
novinosrin
Tourmaline | Level 20


data dat;
   input id ind01 ind02 ind03 ind04 ind05 ind06;
   datalines;
   1 0 0 0 0 4 0
   2 0 0 1 0 3 0 
   3 4 4 4 4 4 4
   4 1 2 3 2 2 2
   5 1 3 0 0 2 0 
   6 1 2 3 4 2 2
   ;
run;
/*Not sure of the group=C*/
data want;
set dat;
array t ind:;
if countc(cats(of t(*)),'04')=dim(t) then group='A';
else if countc(cats(of t(*)),'123')=dim(t) then group='B';
else if countc(cats(of t(*)),'04123')=dim(t) then group='C';
run;
PaigeMiller
Diamond | Level 26
data want;
input id ind01 ind02 ind03 ind04 ind05 ind06;
array ind ind01-ind06;
zero=0;
four=0;
do i=1 to dim(ind);
zero=zero+(ind(i)=0);
four=four+(ind(i)=4);
end;
if zero+four=6 then group='A';
else if zero+four=0 then group='B';
else group='C';
drop i;
datalines;
1 0 0 0 0 4 0
2 0 0 1 0 3 0 
3 4 4 4 4 4 4
4 1 2 3 2 2 2
5 1 3 0 0 2 0
6 1 2 3 4 2 2
;
run;
--
Paige Miller

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
  • 1489 views
  • 0 likes
  • 3 in conversation