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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 2 replies
  • 719 views
  • 3 likes
  • 3 in conversation