Hello SAS Community,
I need your help for the following problem.
Dataset1;
ID Var1
123 0
123 0
123 1
123 1
456 0
456 0
456 0
456 0
789 0
789 1
789 1
789 0
The output datasets should look like
ID Var1
123 0
123 0
789 0
ID Var1
123 1
123 1
789 1
789 1
789 0
Logic:
1) Include only if the ID has at least one occurrence of 1 under var1.
Inferring some unwritten rules, based on the specified results:
data before_1 following_1;
total_1 = 0;
do until (last.id);
set have;
by id;
total_1 + var1;
end;
if total_1 then output_flag='Y';
total_1=0;
do until (last.id);
set have;
by id;
total_1 + var1;
output_flag='Y' then do;
if total_1=0 then output before_1;
else output following_1;
end;
end;
drop total_1 output_flag;
run;
do you mean:
data want;
set have;
if var1=1 then output true;
else output false:
run;
@novinosrin ID 456 is not included, so not quite.
@Reeza Lol so sorry. You are right. I haven't had my lunch yet. Too much of coffee makes me giddy 🙂
Inferring some unwritten rules, based on the specified results:
data before_1 following_1;
total_1 = 0;
do until (last.id);
set have;
by id;
total_1 + var1;
end;
if total_1 then output_flag='Y';
total_1=0;
do until (last.id);
set have;
by id;
total_1 + var1;
output_flag='Y' then do;
if total_1=0 then output before_1;
else output following_1;
end;
end;
drop total_1 output_flag;
run;
why does the second output dataset example include-
ID Var1
123 1
123 1
789 1
789 1
789 0 /* is this correct?*/
data have;
input ID Var1;
datalines;
123 0
123 0
123 1
123 1
456 0
456 0
456 0
456 0
789 0
789 1
789 1
789 0
;
data true false;
call missing(temp);
do until(last.id);
set have;
by id;
if var1 then temp=var1;
end;
do until(last.id);
set have;
by id;
if temp then do;
if var1 then temp1=var1;
if temp1 then output true;
else output false;
end;
end;
drop temp:;
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.
Find more tutorials on the SAS Users YouTube channel.