hi all,
encounter a problem. is it possible to grab an observation and its upper row together?
for example, if i want to draw out harry and peter.
Name Good student
Ben
Peter
Harry Y
Alice
is it always one way to do it like following code?
data want;
set have;
if name in ("peter" , "harry");
run;
can i write a program with designed pattern like
Extract data which name is harry and N-2 observation
and the output should like this:
Name Good student
Ben
Harry Y
thanks in advance
Harry
Hi,
for your setup I would consider something like that:
data have;
infile cards missover;
input Name $ Good_student $ 1.;
cards;
Ben
Peter
Harry Y
Alice
;
run;
data want;
set have curobs=curobs;
if Good_student = "Y" then
do point = max(curobs-1,1) to curobs;
set have point=point;
output;
end;
run;
proc print;
run;
but it doesn't cover situations when you have for example two consecutive "good students", i.e. "Y" in row N and N+1.
All the best
Bart
Do you want to look for Harry or any good student? He may not be the only one 🙂
Hi,
for your setup I would consider something like that:
data have;
infile cards missover;
input Name $ Good_student $ 1.;
cards;
Ben
Peter
Harry Y
Alice
;
run;
data want;
set have curobs=curobs;
if Good_student = "Y" then
do point = max(curobs-1,1) to curobs;
set have point=point;
output;
end;
run;
proc print;
run;
but it doesn't cover situations when you have for example two consecutive "good students", i.e. "Y" in row N and N+1.
All the best
Bart
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.