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

Hello,

I need some help with an array or do loop. 

Here is an example of the data I have:

idlocationyes_no
a10
a20
a30
a40
a50
b10
b21
b32
b41
b50
c10
c20
c30
c40
c50
d10
d20
d30
d40
d50
e10
e22
e30
e40
e50

 

 

What I want is a new column that outputs something only if all rows for the same id have 0's in the yes_no column. so if by the last  row for a particular id, there have only been zeros, I need to flag that id. 

 

So the new data would look something like this:

idlocationyes_nocheck
a10 
a20 
a30 
a40 
a50yes
b10 
b21 
b32 
b41 
b50 
c10 
c20 
c30 
c40 
c50yes
d10 
d20 
d30 
d40 
d50yes
e10 
e22 
e30 
e40 
e50 
1 ACCEPTED SOLUTION

Accepted Solutions
A_Kh
Barite | Level 11
data have;
	input id $	location	yes_no;
	cards;
a	1	0
a	2	0
a	3	0
a	4	0
a	5	0
b	1	0
b	2	1
b	3	2
b	4	1
b	5	0
c	1	0
c	2	0
c	3	0
c	4	0
c	5	0
d	1	0
d	2	0
d	3	0
d	4	0
d	5	0
e	1	0
e	2	2
e	3	0
e	4	0
e	5	0
;

data want;
	set have;
	by id;
	retain temp;
	if first.id then temp=yes_no;
	else temp+yes_no;
	if last.id and temp=0 then check='Yes';
	drop temp; 
proc print;run; 

View solution in original post

6 REPLIES 6
Quentin
Super User

Can you show the code you have tried?  Have you thought about using BY-group processing in a DATA step?  Or perhaps PROC SQL?  Showing the code you tried, and describing why it's not working (error messages? wrong result?) will help people help you.

PaigeMiller
Diamond | Level 26

Although there are solutions that involve do-loops, they are not needed here. I can't possibly see how ARRAYs would be of use here.

 

proc summary data=have nway;
    class id;
    var yes_no;
    output out=_sums_ sum=sum_yes_no;
run;
data want;
    merge have _sums_(keep=id sum_yes_no);
    by id;
    if sum_yes_no=0 and last.id then check=1;
run;

 

 

 

Note: this method can fail if the variable yes_no can take on negative values. 

--
Paige Miller
A_Kh
Barite | Level 11
data have;
	input id $	location	yes_no;
	cards;
a	1	0
a	2	0
a	3	0
a	4	0
a	5	0
b	1	0
b	2	1
b	3	2
b	4	1
b	5	0
c	1	0
c	2	0
c	3	0
c	4	0
c	5	0
d	1	0
d	2	0
d	3	0
d	4	0
d	5	0
e	1	0
e	2	2
e	3	0
e	4	0
e	5	0
;

data want;
	set have;
	by id;
	retain temp;
	if first.id then temp=yes_no;
	else temp+yes_no;
	if last.id and temp=0 then check='Yes';
	drop temp; 
proc print;run; 
eawh100
Obsidian | Level 7
thanks, this worked!
Kurt_Bremser
Super User

Single DATA step method:

data want;
set have;
by id;
retain flag;
if first.id then flag = 1;
if yes_no ne 0 then flag = 0;
if last.id and flag then check = "yes";
drop flag;
run;
eawh100
Obsidian | Level 7
Thanks, this works too!

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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
  • 6 replies
  • 1743 views
  • 3 likes
  • 5 in conversation