BookmarkSubscribeRSS Feed
sasprogramming
Quartz | Level 8

I have the current dataset in SAS:

IDDatePoints
11/01/20190
21/01/2019-200
31/01/20190
31/01/20190
31/01/20190
31/01/20190
31/01/20190
31/01/20190
31/01/20190
31/01/2019-100
31/01/2019-100
31/01/2019-100
31/01/2019-100
31/01/2019-100
31/01/2019-100
31/01/2019-100
31/01/2019-300
31/01/2019-100
31/01/2019-100
31/01/2019-100
41/01/2019-200
51/01/20190
51/01/2019-200
51/01/2019-100

 

I want to create a new dataset with only one entry for each ID, and for this ID I want to choose the minimum value for points for each ID. My desired result looks like this:

 

IDDatePoints
11/01/20190
21/01/2019-200
31/01/2019-300
31/01/20190
41/01/2019-200
51/01/2019-200

 

Is there a way to using proc sql in SAS?

 

Thanks!

6 REPLIES 6
Jagadishkatam
Amethyst | Level 16

Please try

 

proc sql;
create table want as select id,date,min(points) as points
from have group by id,date;
quit;
Thanks,
Jag
Patrick
Opal | Level 21

Below two options (code not tested).

/* option 1 */
proc sql;
  create table want as 
  select id,date,points
  from have 
  group by id
  having min(points)=points
  ;
quit;

/* option 2 */
proc sort data=have;
  by id points;
run;

data want;
  set have;
  by id;
  if first.id;
run;

@Jagadishkatam 

I believe the SQL you've posted won't return the desired result. 

Jagadishkatam
Amethyst | Level 16

@Patrick , seems like the below code is working I tested the same per the data, I believe for the record id 3 we should have only one row of -300 as it is the minimum values under ID 3

 

data have;
input ID Date:ddmmyy10. Points;
cards;
1 1/01/2019  0
2 1/01/2019  -200
3 1/01/2019  0
3 1/01/2019  0
3 1/01/2019  0
3 1/01/2019  0
3 1/01/2019  0
3 1/01/2019  0
3 1/01/2019  0
3 1/01/2019  -100
3 1/01/2019  -100
3 1/01/2019  -100
3 1/01/2019  -100
3 1/01/2019  -100
3 1/01/2019  -100
3 1/01/2019  -100
3 1/01/2019  -300
3 1/01/2019  -100
3 1/01/2019  -100
3 1/01/2019  -100
4 1/01/2019  -200
5 1/01/2019  0
5 1/01/2019  -200
5 1/01/2019  -100
;

proc sql;
create table want as select id,date,min(points) as points
from have group by id,date;
quit;

image.png

Thanks,
Jag
Patrick
Opal | Level 21

@Jagadishkatam 

The issue with your code doesn't surface with the sample data provided. Run with below and you'll see what I'm talking about.

data have;
  input ID Date:ddmmyy10. Points;
  format date date9.;
  cards;
3 1/01/2019  0
3 2/01/2019  -300
3 1/01/2019  -100
3 3/01/2019  -200
;

proc sql;
/*  create table want as */
    select id,date,min(points) as points
    from have 
    group by id,date
    ;
quit;

proc sql;
/*  create table want as */
  select id,date,points
  from have 
  group by id
  having min(points)=points
  ;
quit;
Jagadishkatam
Amethyst | Level 16
I agree with you @Patrick
Thanks,
Jag
Astounding
PROC Star
Why force SQL to do the job, when SAS has already created a tool for this purpose?

proc summary data=have nway;
var points;
class id date;
output out=want (drop=_type_ _freq_) min= ;
run;