input table
ID Month JAN20 FEB20 MAR 20 ARP20 MAY20 JUN20
1 May20 161 105 90 111 10 99
2 Jun 20 38 75 108 95 151 66
3 May20 200 80 225 22 51 99
4 Apr20 101 109 15 65 88 50
output
ID Month T1 T2 T3
1 May20 105 95 111
2 Jun 20 108 95 151
3 May20 80 225 22
4 Apr 101 109 15
In the random sampling some , get last three month sore to respective player id.
for e. g for id =1 , MAY 20 should give last 3 score of FEB20,MAR20,APR20
data have;
input  ID  Month : monyy7.       JAN20   FEB20   MAR20  APR20   MAY20  JUN20;
format Month  monyy7. ;
cards;
1    May20       161       105          90           111         10            99
2   Jun20         38         75           108          95         151           66
3   May20         200       80          225          22          51            99
4   Apr20          101    109         15            65            88             50
;
data want;
 set have;
 t1=vvaluex(put(intnx('month',month,-3),monyy5. -l));
 t2=vvaluex(put(intnx('month',month,-2),monyy5. -l));
 t3=vvaluex(put(intnx('month',month,-1),monyy5. -l));
run;This might be a good case for arrays, especially as the number of available or desired months increases:
data have;
input  ID  Month : monyy7.       JAN20   FEB20   MAR20  APR20   MAY20  JUN20;
format Month  monyy7. ;
cards;
1    May20       161       105          90           111         10            99
2   Jun20         38         75           108          95         151           66
3   May20         200       80          225          22          51            99
4   Apr20          101    109         15            65            88             50
;
data want (drop=i);
 set have;
 array cutoff {6} _temporary_ ("01jan2020"d,"01feb2020"d,"01mar2020"d,"01apr2020"d,"01may2020"d,"01jun2020"d );
 array months{6} JAN20--JUN20;
 array t {3};
 do i=1 to 3;
   t{i}= months{whichn(month,of cutoff{*})-4+i};
 end;
run;It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.
