DATA Step, Macro, Functions and more

Choosing minimum value on matching variable

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 123
Accepted Solution

Choosing minimum value on matching variable

I have this dataset:

 

data x;
  infile datalines;
  input id r2d2 price;
  datalines;
15 12 10
15 12 14
22 17 12
88 16 23
22 17 10
22 15 34
88 15 95
;;
run;

What I'd like to do is - for observations that have the same value for r2d2, I want to output only the observation with the lowest price. How would I go about doing this


Accepted Solutions
Solution
‎03-28-2017 04:09 PM
Trusted Advisor
Posts: 1,913

Re: Choosing minimum value on matching variable

Posted in reply to JediApprentice

An SQL solution

 

--- UNTESTED CODE ----

proc sql;
    create table new as select * from x group by r2d2 having price=min(price);
quit;

View solution in original post


All Replies
Super User
Posts: 19,789

Re: Choosing minimum value on matching variable

Posted in reply to JediApprentice

Sort the data so that the smallest value is first and then use SAS FIRST logic to keep the record. 

Solution
‎03-28-2017 04:09 PM
Trusted Advisor
Posts: 1,913

Re: Choosing minimum value on matching variable

Posted in reply to JediApprentice

An SQL solution

 

--- UNTESTED CODE ----

proc sql;
    create table new as select * from x group by r2d2 having price=min(price);
quit;
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 138 views
  • 1 like
  • 3 in conversation