DATA Step, Macro, Functions and more

one var out of synch

Reply
Contributor
Posts: 32

one var out of synch

I have the table below being generated and I'm looking to exchange the null in var1 for obs 1 to 1. Basically I need to move the data for var1 up one observation and then drop obs 4.

any ideas on the simplest way to complete this task?

obs var1 var2 var3
1 null 22 33
2 1 22 33
3 2 22 33
4 3 0 0

Thanks

Message was edited by: veblen

Message was edited by: veblen Message was edited by: veblen
Super Contributor
Super Contributor
Posts: 3,174

Re: one var out of synch

Look at using a DATA step and SET parameter options to process your file backwards.

Or, another option, take special variable _N_ and assign it to a variable of your own, then use that to re-sort the file. Then you can use the LAG function to transfer SAS variable values to an adjacent observation. When complete, then re-sort your file back into original sequence.

Scott Barry
SBBWorks, Inc.
Contributor
Posts: 32

Re: one var out of synch

Thanks Scott!!!

I appreciate the Friday response.

I think I will look at the second approach. I neglected to show the before and after in my initial post. Plus I used the same values for vars 2 and 3 but they are different.

table A
obs var1 var2 var3
1 null 22 33
2 1 23 34
3 2 24 35
4 3 0 0

table B
obs var1 var2 var3
1 1 22 33
2 2 23 34
3 3 24 35
4 x 0 0
PROC Star
Posts: 7,487

Re: one var out of synch

Veblen,

Even less work, take a look at the notion of look ahead look back as described at:
http://www.sascommunity.org/wiki/Look-Ahead_and_Look-Back

Of course, given your current need, you only have to use the look ahead portion and, for the last record, either delete it or set the value to missing.

HTH,
Art
Ask a Question
Discussion stats
  • 3 replies
  • 113 views
  • 0 likes
  • 3 in conversation