SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

rearranging data to select ID with min value of a variable

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

rearranging data to select ID with min value of a variable

I have a dataset that looks like this -

data try_1;

input ID     c_ID     d_status     age     fin_grp;

datalines;

1    1    1    6    1

1    2    1    12    1

1    3    1    15    1

2    1    2    10    0

2    2    2    14    0

2    3    2    16    0

4    1    1    6    1

4    2    2    13    1

4    3    2    15    1

5    1    1    9    0

5    2    2    12    0

5    3    2    11    0

;

run;

For each group, I want to select one c_id per ID in two stages.

In the first stage, I need to decide among d_status values based on value for fin_grp. 

Thus for fin_grp=1, I need to select from d_status=1 AND for fin_grp=0, I need to select from d_status not equal to 1.

In the second stage, withing the above selected d_status, want to select a one c_id with has minimum age.

eg. For ID=5, I would like to select c_id=3 which has d_status=2 and minimum age.

Can anyone help?


Accepted Solutions
Solution
‎01-15-2015 03:09 AM
Super User
Posts: 9,671

Re: rearranging data to select ID with min value of a variable

data try_1;
input ID     c_ID     d_status     age     fin_grp;
datalines;
1    1    1    6    1
1    2    1    12    1
1    3    1    15    1
2    1    2    10    0
2    2    2    14    0
2    3    2    16    0
4    1    1    6    1
4    2    2    13    1
4    3    2    15    1
5    1    1    9    0
5    2    2    12    0
5    3    2    11    0
;
run;
proc sql;
 create table want as
  select *
   from try_1
    where (fin_grp=1 and d_status=1) or (fin_grp=0 and d_status ne 1)
      group by id
       having age=min(age);
quit;


Xia Keshan

View solution in original post


All Replies
Solution
‎01-15-2015 03:09 AM
Super User
Posts: 9,671

Re: rearranging data to select ID with min value of a variable

data try_1;
input ID     c_ID     d_status     age     fin_grp;
datalines;
1    1    1    6    1
1    2    1    12    1
1    3    1    15    1
2    1    2    10    0
2    2    2    14    0
2    3    2    16    0
4    1    1    6    1
4    2    2    13    1
4    3    2    15    1
5    1    1    9    0
5    2    2    12    0
5    3    2    11    0
;
run;
proc sql;
 create table want as
  select *
   from try_1
    where (fin_grp=1 and d_status=1) or (fin_grp=0 and d_status ne 1)
      group by id
       having age=min(age);
quit;


Xia Keshan

New Contributor
Posts: 2

Re: rearranging data to select ID with min value of a variable

Thanks a lot.

That was really helpful

Frequent Contributor
Posts: 115

Re: rearranging data to select ID with min value of a variable

data try_1;

input ID     c_ID     d_status     age     fin_grp;

datalines;

1    1    1    6    1

1    2    1    12    1

1    3    1    15    1

2    1    2    10    0

2    2    2    14    0

2    3    2    16    0

4    1    1    6    1

4    2    2    13    1

4    3    2    15    1

5    1    1    9    0

5    2    2    12    0

5    3    2    11    0

;

data want (drop=age1 c_id1);

age1=.;

do until(last.id);

set try_1;

by id;

where (fin_grp=1 and d_status=1) or (fin_grp=0 and d_status ne 1);

age1= min(age1,age);

if age<=age1 then c_id1=c_id;

else if age>age1 then c_id=c_id1;

end;

age=age1;

run;

☑ This topic is SOLVED.

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

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