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