BookmarkSubscribeRSS Feed
Aexor
Lapis Lazuli | Level 10

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 

2 REPLIES 2
Ksharp
Super User
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;
mkeintz
PROC Star

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;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

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

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 2 replies
  • 534 views
  • 3 likes
  • 3 in conversation