Help using Base SAS procedures

Moving one row up

Reply
Contributor
Posts: 37

Moving one row up

I am havig a variable like this 

no

1

2

3

4

5

I want to create a new varible to move uprow up..

no New_no

1  2

2  3

3  4

4  5

5  .

PROC Star
Posts: 7,473

Moving one row up

here is one way:

data have;

  input no;

  cards;

1

2

3

4

5

;

data want;

  set have end=lastone;

  set have ( firstobs = 2 keep = No rename = (No = New_No) )

      have (      obs = 1 drop = _all_                     );

New_No = ifn(  lastone, (.), New_No );

run;

Valued Guide
Posts: 765

Re: Moving one row up

Hi ... another approach ...

data want;

merge have have (firstobs=2 rename=(no=new_no));

run;

Occasional Contributor
Posts: 7

Re: Moving one row up

If the data set contains only these two variables, I would do it like this:

*step 1. Delete the first row;

data output1;

set input;

if first.no then delete;

rename no=new_no;

run;

*step 2. Build new variable;

data output2;

set ouput1;

no=lag(new_no);

run;

Then deal with the first obs of no and last obs of new_no.

HTH!

Ask a Question
Discussion stats
  • 3 replies
  • 3143 views
  • 1 like
  • 4 in conversation