BookmarkSubscribeRSS Feed
hhchenfx
Rhodochrosite | Level 12

Hi Everyone,

I have a data with 2 variables value and N (N is the lag step used below)

For each record (i), I have to calculate the Change= value – lagN(value) for the prior 10 record. The N step is the N of current record.

So the below is how the calculation is done for record  11th (with lag step =2). the 3rd column is lag2 value, the 4th column is the change.

Then I have to find the percentile of current change among these prior 10 prior change value.

(the most challenging part for me is to create a sub group for each record to do the rank, In practice, I have to find the percentile based on the prior 500 record)

Could you please help me to do that?

Thank you so much.

HHC

  2 2  .

  3 3  .

  9 4 9-2=7

  4 6 3 4-3=1

  5 8 9

  6 9 4

  7 1 5

  8 5 6

  6 2 7

  7 8 8

  8 2 6

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

data have;

  input value N;

  datalines;

  2 2

  3 3

  9 4

  4 6

  5 8

  6 9

  7 1

  8 5

  6 2

  7 8

  8 2

  3 4

  9 6

  0 9

  6 4

  4 8

  9 9

  8 3

  7 3

  4 4

  5 7

  4 5

  32 2

  21 1

  23 2

  4 3

  5 4

  6 6

  7 8

  8 2

;run;

3 REPLIES 3
Ksharp
Super User

Can't use PCTL Function to get it ? What does your output look like ?

hhchenfx
Rhodochrosite | Level 12

Hi Xia,

For each record, in the output we should have a new percentile collumn.

the difficulty for me is that the percentile of record ( i ) is built on the difference with lag step ( i )

I will create a spreadsheet when i get my laptop with me.

Thank you,

HHC

ballardw
Super User

You also may want to look at the DIFn function. Does the Lag and subtraction as one function.

And you only need the first variable from what you are showing.

If I understand what you may be looking for:

data want;

     set have;

     col2=dif2(n);

     col3=dif3(n);

     col4=dif4(n);

     col5=dif5(n);

     col6=dif6(n);

     col7=dif7(n);

     col8=dif8(n);

     col9=dif9(n);

     col10=dif10(n);

run;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1195 views
  • 0 likes
  • 3 in conversation