BookmarkSubscribeRSS Feed
Barney1998
Obsidian | Level 7

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).

Thanks in advance,

Barney   

3 REPLIES 3
AMSAS
SAS Super FREQ

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 ;
ballardw
Super User

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.

mkeintz
PROC Star

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

--------------------------

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 306 views
  • 1 like
  • 4 in conversation