BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
hhchenfx
Barite | Level 11

Hi Everyone,

I want to do a process similar to finding moving percentile.

However, for each record, I want to look back 500 records (excluding the current record) and extract the value of the 475th largest record (it is not the same as 95 percentile in SAS).

I prefer this way to percentile since it is comparable with other software I use and I can control the value better.

So for the data below, for the first record, the value-475 should be 476.

Thank you for your help.

HHC

data have;

do value=1 to 1000;output; end;

;run;

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

You mean look forward ?

data have;
     do value=1 to 1000;
           output;
     end;
     ;
run;

data want;
 set have nobs=nobs;
 array x{99999} _temporary_;
 do i=_n_+1 to min(nobs,_n_+499);
  set have(rename=(value=v)) point=i;
  x{i}=v;
 end;
 _25th=largest(25,of x{*});
 call missing(of x{*});
 drop i v;
run;

Xia Keshan

View solution in original post

5 REPLIES 5
Haikuo
Onyx | Level 15

I think you mean 25th largest?

data have;

     do value=1 to 1000;

           output;

     end;

     ;

run;

data want;

     array t(0:499) _temporary_;

     set have;

     t(mod(_n_,500))=value;

     if _n_>499 then

           _25th=largest(25,of t(*));

run;

Haikuo

hhchenfx
Barite | Level 11

Hi,

It is almost what I want.

However, the first record value for _25th should be 476.

file want should look like:

value     _25th

1          476

2          477

...

HHC

Haikuo
Onyx | Level 15

This?

data want;

array t(0:499) _temporary_;

set have;

t(mod(_n_,500))=value;

if _n_>499 then do;

_25th=largest(25,of t(*));

n+1;

output;

end;

drop value;

rename n=value;

run;

hhchenfx
Barite | Level 11

Yes it is the what I want. But I want to keep the original 1000 records. Right now the last 499 records are gone.

If I have to do DO LOOP through this code, each time, the data is reduced by 499.

HHC

Ksharp
Super User

You mean look forward ?

data have;
     do value=1 to 1000;
           output;
     end;
     ;
run;

data want;
 set have nobs=nobs;
 array x{99999} _temporary_;
 do i=_n_+1 to min(nobs,_n_+499);
  set have(rename=(value=v)) point=i;
  x{i}=v;
 end;
 _25th=largest(25,of x{*});
 call missing(of x{*});
 drop i v;
run;

Xia Keshan

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
  • 707 views
  • 6 likes
  • 3 in conversation