BookmarkSubscribeRSS Feed
NKormanik
Barite | Level 11

Here's one that Excel can do, so I'll use Excel if no fairly easy SAS way is possible.

Note following portion of data set:

ComboVar_1Var_2
20101_216030.-1.94930
20101_216030.-1.80663
20101_216030.-1.66396
20101_216030.-1.52129
21603_20101-0.807940.
21603_20101-0.665270.
21603_20101-0.522600.
21603_20101-0.379930.

Focus only on the Var_1 and Var_2 columns..., at least for the time being.

I need to "swap" the contents of the bottom four rows of cells.

Does SAS have a way to do this -- without your having to write tons of code?

Thanks much.

Nicholas Kormanik

2 REPLIES 2
RichardinOz
Quartz | Level 8

There is no data step swap function (I've occasionally wished for it) but you can temporarily create another column say temp, then use code such as

If (condition to select rows to swap)

  then do ;

temp = Var_1 ;

Var_1 = Var2 ;

Var_2 = temp ;

end ;

drop temp ;

You can also do it in sql, without creating a temp column but it is a lot more verbose.

Richard

LinusH
Tourmaline | Level 20

Unclear where you write the line for tons Smiley Wink, but here goes (assuming that you need to store column with values <> 0 in one column):

proc sql;

     update mytable

          set var_2 = var_1

          where var_1 <> 0 and var_2 = 0

     ;

quit;

Comparing to Excel is puzzling me. Excel is great when doing minor ad-hoc stuff, SAS is suited for developing programs that can be used multiple times, in batch etc.

Data never sleeps

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