BookmarkSubscribeRSS Feed
MattM
Calcite | Level 5
I have a large sequential data set. I have identified a small subset of extreme observations in the set. What I want to do is extract the extreme observations and the 10 observation before and after each extreme observations. If that is possible, I would also like to ensure that duplicates are removed as well. And ideally, since I have by variables, I would love to be able to abide by the first.var and last.var parameters so as not to include an obs from a different by variable within the range. Can anyone help?
3 REPLIES 3
ArtC
Rhodochrosite | Level 12
Here is one solution. It requires two passes of the primary data set 😞 . If this is too big of an inefficiency there is probably a HASH table solution. try it and let us know.
Art
[pre]
* build set of extreme values - you already have these;
data select(keep=region subsidiary product stores);
set sashelp.shoes(where=(stores>30));
run;

* First pass of data to determine obs numbers of the extreme values;
data cnt(keep=region subsidiary product stores cnt
rename=(region=cregion subsidiary=csubsidiary product=cproduct stores=cstores));
merge sashelp.shoes(in=inshoes)
select(in=inselect);
by region subsidiary product;
cnt+1;
if inshoes and inselect;
run;

* select leading and trailing obs;
* retrieve +/- 3 obs;
data surrounded(keep=region subsidiary product stores sales );
* array to hold used obs numbers - dimension depends on the total number of obs;
array used {100000] $1 _temporary_;
set cnt;
do point=(cnt-3) to (cnt+3);
if 1 le point le nobs then do;
set sashelp.shoes point=point nobs=nobs;
* PRODUCT is not included in the comparison so duplicate obs are possible without checking array;
if used{point}=' ' & region=cregion & subsidiary=csubsidiary then do;
output surrounded;
used{point} = 'x';
end;
end;
end;
run;
[/pre] Message was edited by: ArtC
Ksharp
Super User
Hi. Can you give some what your dataset look like with some dummy data,
and what output you would like to be . giving a example would help understand what your purpose.
Flip
Fluorite | Level 6
Try this. You will want to check for end of file etc to avoid warnings, but this works in the simple example I wrote.


Data new;
set old;
counter+1;
chkvar = byvar;
if (condition met) then do pv = counter -10 to counter + 10;
set old point = pv;
if byvar = chkvar then output;
end;
run;

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