BookmarkSubscribeRSS Feed
wanttolearnSAS
Calcite | Level 5

StudentId  Addpdr Subject GradeC ExamDate    Marks

001             IN         PHY       X1      22/03/2017     73

001             PK        CHE      X11     02/03/2016     82

001             IN         PHY       X1      22/03/2017     62

001             IN         PHY       X1      10/11/2016     87

001             IN         PHY       X1      23/03/2017     92

001             PK        CHE       X11    10/03/2017     51

002             BN        PHY       X1      27/03/2017     62

002             BN        PHY       X11    02/03/2017     98

002             BN        PHY       X1      27/03/2017     34

002             BN        PHY       X1      27/03/2017     53

 

The desired output is:

StudentId  Addpdr  GradeC  Subject   Marks

001            IN           X1          PHY        135

001            PK         X11         CHE         51

002            BN         X1          PHY        149

002            BN        X11         PHY          98

 

The Marks in the output is the addition of the marks if the latest exam date (with the combination of Studentid,Addpdr and GradeC) is repeating else the marks is the marks of the latest exam date for the combination of Studentid,Addpdr and GradeC.

 

 

3 REPLIES 3
Reeza
Super User

What have you tried so far?

 

I would suggest using PROC MEANS to summarize along the grouping variables you've identified and then to use PROC SORT to take the latest record using the NODUPKEY option. If you post what you've tried so far we can help fix/modify your code as necessary.

 


@wanttolearnSAS wrote:

StudentId  Addpdr Subject GradeC ExamDate    Marks

001             IN         PHY       X1      22/03/2017     73

001             PK        CHE      X11     02/03/2016     82

001             IN         PHY       X1      22/03/2017     62

001             IN         PHY       X1      10/11/2016     87

001             IN         PHY       X1      23/03/2017     92

001             PK        CHE       X11    10/03/2017     51

002             BN        PHY       X1      27/03/2017     62

002             BN        PHY       X11    02/03/2017     98

002             BN        PHY       X1      27/03/2017     34

002             BN        PHY       X1      27/03/2017     53

 

The desired output is:

StudentId  Addpdr  GradeC  Subject   Marks

001            IN           X1          PHY        135

001            PK         X11         CHE         51

002            BN         X1          PHY        149

002            BN        X11         PHY          98

 

The Marks in the output is the addition of the marks if the latest exam date (with the combination of Studentid,Addpdr and GradeC) is repeating else the marks is the marks of the latest exam date for the combination of Studentid,Addpdr and GradeC.

 

 


 

art297
Opal | Level 21

Here is one way:

data have;
  input (StudentId  Addpdr Subject GradeC) ($) ExamDate :ddmmyy10.  Marks;
  cards;
001             IN         PHY       X1      22/03/2017     73
001             PK        CHE      X11     02/03/2016     82
001             IN         PHY       X1      22/03/2017     62
001             IN         PHY       X1      10/11/2016     87
001             IN         PHY       X1      23/03/2017     92
001             PK        CHE       X11    10/03/2017     51
002             BN        PHY       X1      27/03/2017     62
002             BN        PHY       X11    02/03/2017     98
002             BN        PHY       X1      27/03/2017     34
002             BN        PHY       X1      27/03/2017     53
;

proc sort data=have;
  by StudentID Addpdr GradeC descending ExamDate;
run;

proc summary data=have;
  var Marks;
  by  StudentID Addpdr GradeC descending ExamDate;
  output out=need (rename=(_freq_=count )
                   drop=_type_ ExamDate
                   where=(count ge 2)) sum=sum_m;
run;

data want (drop=count sum_m ExamDate);
  merge have need;
  by StudentID Addpdr GradeC;
  if first.GradeC then do;
    if count ge 2 then Marks=sum_m;
    output;
  end;
run;

Art, CEO, AnalystFinder.com

 

wanttolearnSAS
Calcite | Level 5

I have sorted and summarized and then merged.

It is working.

Thanks a lot.

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 connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

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