BookmarkSubscribeRSS Feed
Sroy9
Calcite | Level 5
This seems like it should have a fairly easy solution, but I have not been able to find it. I want to keep every 2nd row in a data set I have, deleting each odd numbered row. The only way I can think to do this is to create an index spanning the whole set, then delete based on the index, but I'm unsure how to do that.

Any help would be appreciated.
5 REPLIES 5
data_null__
Jade | Level 19
MOD
Returns the remainder from the division of the first argument by the second argument, fuzzed to avoid most unexpected floating-point results

[pre]
data classEven;
set sashelp.class;
if mod(_n_,2) eq 0;
obs = _n_;
run;
proc print;
run;

data classEven;
set sashelp.class;
where mod(monotonic(),2) eq 0;
run;
proc print;
run;
[/pre]
Cynthia_sas
SAS Super FREQ
Hi:
My usual warning about monotonic(), which may work in this case, but is not guaranteed to work in all cases... Any warning like this gives me something to worry about:
http://support.sas.com/kb/15/138.html

A simple alternative (assuming that you are not doing anything other than a simple read and write with the data step program, so that _n_ will correspond to the number of obs in the data set).

cynthia
[pre]
data classSimple;
set sashelp.class;
orig_obs = _n_;
if mod(_n_,2) eq 0 then output;
run;

proc print data=classSimple;
run;
[/pre]
Sroy9
Calcite | Level 5
Thanks, I didn't know the row indexes could be referenced as _n_. Works perfectly now.
data_null__
Jade | Level 19
_N_ is not the row index. It is the data step iteration counter. In this simple situation they are equal, but not always.
Cynthia_sas
SAS Super FREQ
Hi:
Data _NULL_ is correct. _N_ is not a row index -- it is a count of loops through the data step program. Which is why I qualified what I said about using _N_ -- that as long as you were doing a simple read/write in your program you could use _N_. Because in that instance, _N_ will correspond to each observation, assuming that each observation is read with 1 loop of the DATA step program.

You could write DATA step programs to read more than one observation in a single loop of the program -- in which case, _N_ would cease to be a possibility. But in your case, assuming you have explained the problem as your program works -- with a simple read/write situation -- and you are only reading 1 observation for every loop through the program, then you can use _N_ as shown.

cynthia

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