BookmarkSubscribeRSS Feed
veblen
Calcite | Level 5
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
3 REPLIES 3
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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.
veblen
Calcite | Level 5
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
art297
Opal | Level 21
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

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 3 replies
  • 1248 views
  • 0 likes
  • 3 in conversation