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

Hello , 

I'm using SAS EG7.1 , and I have a dataset composed of three columns :  Date, Mat , Tx

for a given date we can have multiple rows with different MAT and TX values.

I want to select the last two available TX values for every MAT value.

The data is sorted by date ascending.

 

My dataset has the following format : 

 

DATEMATTX
10/01/201752,34
13/01/201752,34
13/01/201772,86
17/01/201752,34
20/01/201752,34
24/01/201732,19
24/01/201752,34
26/01/201772,91
27/01/201732,19
27/01/201752,40
31/01/201762,62
31/01/201742,29
03/02/201762,62
03/02/201742,29
07/02/201752,40
07/02/201762,60
07/02/201732,17
10/02/201762,60
10/02/201752,40
10/02/201732,17

The Output I want is : 

 

DATEMATTX
07/02/201732,17
10/02/201732,17
31/01/201742,29
03/02/201742,29
07/02/201752,40
10/02/201752,40
07/02/201762,60
10/02/201762,60
13/01/201772,86
26/01/201772,91

 

Thank you in advance for your help.

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@LoPez_Diaz wrote:

Hello , 

I'm using SAS EG7.1 , and I have a dataset composed of three columns :  Date, Mat , Tx

for a given date we can have multiple rows with different MAT and TX values.

I want to select the last two available TX values for every MAT value.

The data is sorted by date ascending.

 

My dataset has the following format : 

 

DATE MAT TX
10/01/2017 5 2,34
13/01/2017 5 2,34
13/01/2017 7 2,86
17/01/2017 5 2,34
20/01/2017 5 2,34
24/01/2017 3 2,19
24/01/2017 5 2,34
26/01/2017 7 2,91
27/01/2017 3 2,19
27/01/2017 5 2,40
31/01/2017 6 2,62
31/01/2017 4 2,29
03/02/2017 6 2,62
03/02/2017 4 2,29
07/02/2017 5 2,40
07/02/2017 6 2,60
07/02/2017 3 2,17
10/02/2017 6 2,60
10/02/2017 5 2,40
10/02/2017 3 2,17

The Output I want is : 

 

DATE MAT TX
07/02/2017 3 2,17
10/02/2017 3 2,17
31/01/2017 4 2,29
03/02/2017 4 2,29
07/02/2017 5 2,40
10/02/2017 5 2,40
07/02/2017 6 2,60
10/02/2017 6 2,60
13/01/2017 7 2,86
26/01/2017 7 2,91

 

Thank you in advance for your help.

 

 

 


Not particularly fancy but works for your example data. Note that I use decimals instead of commas for numeric values due to NLS.

The first sort, second data step and second sort are the parts you need for your data.

data have;
   input DATE :ddmmyy10. MAT TX  ;
   format date ddmmyy10.;
datalines;
10/01/2017 5 2.34 
13/01/2017 5 2.34 
13/01/2017 7 2.86 
17/01/2017 5 2.34 
20/01/2017 5 2.34 
24/01/2017 3 2.19 
24/01/2017 5 2.34 
26/01/2017 7 2.91 
27/01/2017 3 2.19 
27/01/2017 5 2.40 
31/01/2017 6 2.62 
31/01/2017 4 2.29 
03/02/2017 6 2.62 
03/02/2017 4 2.29 
07/02/2017 5 2.40 
07/02/2017 6 2.60 
07/02/2017 3 2.17 
10/02/2017 6 2.60 
10/02/2017 5 2.40 
10/02/2017 3 2.17 
;
proc sort data=have;
  by mat descending date;
run;
data want;
   set have;
   retain count;
   by mat descending date;
   if first.mat then count=1;
   else count+1;
   if count le 2;
   drop count;
run;
proc sort data=want;
   by mat date;
run;

View solution in original post

4 REPLIES 4
ballardw
Super User

@LoPez_Diaz wrote:

Hello , 

I'm using SAS EG7.1 , and I have a dataset composed of three columns :  Date, Mat , Tx

for a given date we can have multiple rows with different MAT and TX values.

I want to select the last two available TX values for every MAT value.

The data is sorted by date ascending.

 

My dataset has the following format : 

 

DATE MAT TX
10/01/2017 5 2,34
13/01/2017 5 2,34
13/01/2017 7 2,86
17/01/2017 5 2,34
20/01/2017 5 2,34
24/01/2017 3 2,19
24/01/2017 5 2,34
26/01/2017 7 2,91
27/01/2017 3 2,19
27/01/2017 5 2,40
31/01/2017 6 2,62
31/01/2017 4 2,29
03/02/2017 6 2,62
03/02/2017 4 2,29
07/02/2017 5 2,40
07/02/2017 6 2,60
07/02/2017 3 2,17
10/02/2017 6 2,60
10/02/2017 5 2,40
10/02/2017 3 2,17

The Output I want is : 

 

DATE MAT TX
07/02/2017 3 2,17
10/02/2017 3 2,17
31/01/2017 4 2,29
03/02/2017 4 2,29
07/02/2017 5 2,40
10/02/2017 5 2,40
07/02/2017 6 2,60
10/02/2017 6 2,60
13/01/2017 7 2,86
26/01/2017 7 2,91

 

Thank you in advance for your help.

 

 

 


Not particularly fancy but works for your example data. Note that I use decimals instead of commas for numeric values due to NLS.

The first sort, second data step and second sort are the parts you need for your data.

data have;
   input DATE :ddmmyy10. MAT TX  ;
   format date ddmmyy10.;
datalines;
10/01/2017 5 2.34 
13/01/2017 5 2.34 
13/01/2017 7 2.86 
17/01/2017 5 2.34 
20/01/2017 5 2.34 
24/01/2017 3 2.19 
24/01/2017 5 2.34 
26/01/2017 7 2.91 
27/01/2017 3 2.19 
27/01/2017 5 2.40 
31/01/2017 6 2.62 
31/01/2017 4 2.29 
03/02/2017 6 2.62 
03/02/2017 4 2.29 
07/02/2017 5 2.40 
07/02/2017 6 2.60 
07/02/2017 3 2.17 
10/02/2017 6 2.60 
10/02/2017 5 2.40 
10/02/2017 3 2.17 
;
proc sort data=have;
  by mat descending date;
run;
data want;
   set have;
   retain count;
   by mat descending date;
   if first.mat then count=1;
   else count+1;
   if count le 2;
   drop count;
run;
proc sort data=want;
   by mat date;
run;
LoPez_Diaz
Obsidian | Level 7
Hello Ballardw ,
This works great !! Thank you for your fast reply !!
Have a nice day!
novinosrin
Tourmaline | Level 20

Hi @LoPez_Diaz  Too late as I missed to notice your thread, but FWIW

 


data have;
   input DATE :ddmmyy10. MAT TX  ;
   format date ddmmyy10.;
datalines;
10/01/2017 5 2.34 
13/01/2017 5 2.34 
13/01/2017 7 2.86 
17/01/2017 5 2.34 
20/01/2017 5 2.34 
24/01/2017 3 2.19 
24/01/2017 5 2.34 
26/01/2017 7 2.91 
27/01/2017 3 2.19 
27/01/2017 5 2.40 
31/01/2017 6 2.62 
31/01/2017 4 2.29 
03/02/2017 6 2.62 
03/02/2017 4 2.29 
07/02/2017 5 2.40 
07/02/2017 6 2.60 
07/02/2017 3 2.17 
10/02/2017 6 2.60 
10/02/2017 5 2.40 
10/02/2017 3 2.17 
;

proc sql;
create table want(drop=t) as
select *
from
(select *, date<max(date) as t from have group by mat) 
group by mat, t
having max(date)=date and t=1 or t=0
order by mat,date;
quit;
LoPez_Diaz
Obsidian | Level 7
This also works ! Thank you ! I was looking for a sql procedure to it , and this is it !
Thank you again

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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
  • 4 replies
  • 869 views
  • 2 likes
  • 3 in conversation