Obsidian | Level 7

## keep specific obs and its next

Hallo to everyone,

Could someone help me with this?

I have one really big data set ,and from this I wanna to create one new .After the where statement I keep only the observation that are really needed. The problem is ,i want this observations and their exactly next.For example if from the where statement  I keep the observations 1 ,100,and 1000 in the new data set i will have the observations 1,2(1+1),100,101(100+1),1000,1001(1000+1).

Barney

3 REPLIES 3
SAS Super FREQ

## Re: keep specific obs and its next

I don't think a WHERE statement is going to work (assuming the following observation doesn't meet your where clause)

Here's a simple example of how you can do it using an IF statement

``````data have ;
do i=1 to 10 ;
j=mod(i,3) ;
output ;
end ;
run ;

data want ;
retain flag 0 ;
set have ;
if j=0 then do ;
output ;
flag=1 ;
end ;
else if flag=1 then do ;
flag=0 ;
output ;
end  ;
run ;``````
Super User

## Re: keep specific obs and its next

This sounds odd enough that I think you need to provide some example data and the expected results. It does not need to be 1000's or records long but enough to see what is needed.

For example, how do you write your where statement to select 1, 100 and 1000?. You should show the basic code you are currently using to select those records. The entire step.

I suspect that a solution will require more than one step as a Where that selects records will not select any records that do not meet the condition. So if record 100 meets your where condition and 99 does not then the record 99 is not available at that time.

PROC Star

## Re: keep specific obs and its next

Do you really know the observation numbers you want (1, 100, 1000 and their successors)?

If obs num is the guiding criterion, then the WHERE statement will not produce what you want. because observation numbers are generated AFTER the where filter, not before.

But in this situation you can use SET with the POINT= option.

``````%let obslist=1,100,1000;
data want;
do ptr=&obslist;
set have point=ptr;
output;
ptr=ptr+1;
set have point=ptr;
output;
end;
stop;
run;``````

And even if the where filter would be based on some desired variable value, the where filter will not provide the observation following the desired value.   You'll have to use subsetting IF and the LAG function.

``````data want;
set sashelp.class;
if age=15 or lag(age)=15;
run;``````

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Discussion stats
• 3 replies
• 449 views
• 1 like
• 4 in conversation