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

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