BookmarkSubscribeRSS Feed
Demo_2021
Calcite | Level 5

Can I get with writing a programme run through observations and identify instances where the preceding and following observation are the same and the index (middle) observation equals to a specific value i.e. second observation for oo1and third observation for oo7.

 

oo13
oo14
oo13
oo24
oo24
oo24
oo34
oo32
oo34
oo44
oo43
oo42
oo44
oo54
oo54
oo52
oo53
oo52
oo51
oo62
oo62
oo64
oo63
oo62
oo61
oo72
oo72
oo74
oo72
oo72
oo71

 

 

 

2 REPLIES 2
jimbarbour
Meteorite | Level 14

You might want to take a look at the blog post by @LeonidBatkhan called "Hopping for the Best."  In it he explains how to "look ahead" beyond the current record and get values from records that have yet to be read in the main sequential process.  If you use the techniques he describes, you can see all the values on all the records for, say 'oo3'.  

 

https://blogs.sas.com/content/sgf/2017/11/01/hopping-for-the-best-calculations-across-sas-dataset-ob...

 

Jim

quickbluefish
Obsidian | Level 7

You could try this - not tested - might have bugs.  Here, I am calling your dataset 'stuff' and I'm calling your variables 'ID' (for the oo1, oo2... column) and 'value' for the 2nd column.

 

* make a row variable so that you can sort by ID (oo1, oo2, oo3 etc.) 
without ruining the order of the integer variable (which I am calling 'value') ;

data stuff;
set stuff;
row=_N_;
run;

proc sort data=stuff; by id row; run;

proc sql noprint; 
select max(nrows) into :maxrows from (select ID, count(*) as nrows from stuff group by ID);
quit;

* set to whatever value it is you are trying to identify between two matching values ;
%let SEARCHVAL=7;

data stuff;
length row 3;
set stuff (drop=row);
by id;
array T {&maxrows} _temporary_;
array f {&maxrows} _temporary_;
retain row 0;
if first.id then do;
	call missing(of T[*], of f[*]);
	x=0;
end;
x+1;
T[x]=value;
f[x]=0;
if last.id then do;
	i=2;
	do while(i<x);
		i+1;
		if T[i-2]=T[i] and T[i-1]=&SEARCHVAL then f[i-1]=1;
	end;
	do i=1 to x;
		row+1;
		value=T[x];
		flag=f[x];
		output;
	end;
end;
keep row ID value flag;
run;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 639 views
  • 1 like
  • 3 in conversation