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

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
Lapis Lazuli | Level 10
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.

BASUG is hosting free webinars Next up: Don Henderson presenting on using hash functions (not hash tables!) to segment data on June 12. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
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
Lapis Lazuli | Level 10
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
Fluorite | Level 6
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
Fluorite | Level 6
Thanks, this works too!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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