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

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

1 ACCEPTED SOLUTION

Accepted Solutions
yabwon
Onyx | Level 15

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

 

 

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



View solution in original post

4 REPLIES 4
Satish_Parida
Lapis Lazuli | Level 10
There is a function called LAG can be used in your situation. Can you please give a better example and also explain if the GOOD STUDENT flag affecting your selection. This example is not clear enough.
PeterClemmensen
Tourmaline | Level 20

Do you want to look for Harry or any good student? He may not be the only one 🙂

yabwon
Onyx | Level 15

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

 

 

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



harrylui
Obsidian | Level 7
THANKS this is what i need

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 4 replies
  • 859 views
  • 0 likes
  • 4 in conversation