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

I have been asked to get the matched keyword from a text file and get 2 lines/observations before and 2 lines/observations after.My data looks like this

data have;

input var$25.;

Description

This is a

format

for

the

variable

named

Default

it is changed

in release 5

change was

logged on 18 Jan 2015

;

run;

What I want in output is

variable
named
Default
it is changed
in release 5

Here , i tried using the Index function something like this

Data want;

set have;

if index(var,"default") >1 then output;

run;

But it is producing just 1 line, however i want 2 lines above and 2 lines below.

Please suggest some alternatives

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

Assuming that your "IF" condition properly selects the records you want:

data want;

   set have nobs=_totalobs_;

   if index(var, "default") then do _k_ = max(1, _n_-2) to min(_totalobs_, _n_+2);

      set have point=_k_;

      output;

   end;

run;

You may need to adjust the IF condition to handle variations in spelling, such as uppercase vs. lowercase.  Also, this outputs 5 lines:  the line itself, as well as 2 before and 2 after.  If you wanted to eliminate the line itself, that would be possible with minor changes:

if _n_ ne _k_ then output;

Good luck.

Good luck.

View solution in original post

2 REPLIES 2
Astounding
PROC Star

Assuming that your "IF" condition properly selects the records you want:

data want;

   set have nobs=_totalobs_;

   if index(var, "default") then do _k_ = max(1, _n_-2) to min(_totalobs_, _n_+2);

      set have point=_k_;

      output;

   end;

run;

You may need to adjust the IF condition to handle variations in spelling, such as uppercase vs. lowercase.  Also, this outputs 5 lines:  the line itself, as well as 2 before and 2 after.  If you wanted to eliminate the line itself, that would be possible with minor changes:

if _n_ ne _k_ then output;

Good luck.

Good luck.

Haikuo
Onyx | Level 15

Or construct an sequential index upfront:

data have;

     input var $25.;

     n=_n_;

     cards;

Description

This is a

format

for

the

variable

named

Default

it is changed

in release 5

change was

logged on 18 Jan 2015

;

run;

proc sql;

     create table want as

           select b.var

                from have (where=(find(var,'Default')>0)) a

                     left join have b

                           on b.n between a.n-2 and a.n+2

                     order by b.n;

quit;

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1007 views
  • 3 likes
  • 3 in conversation