BookmarkSubscribeRSS Feed
Shilin
Calcite | Level 5

Hi, i am looking to create additional 3 columns from the input table below to give the earliest grade change if an ID falls into group 2.

For example for ID = a1, it was classified as Group 2 on 1 Feb 2015 and the earliest change in grade happens on 1 Apr 2015 when it changes to 4. I would also like to calculate the number of months it took to change from grade 3 to grade 4.

i have also attached the desired output table below.

 

Thanks!

 

Input table

IDDateGradeGroup
a11-Jan-1531
a11-Feb-1532
a11-Mar-1531
a11-Apr-1542
b11-Jan-1552
b11-Feb-1552
b11-Mar-1541
c11-Jan-1572
c11-Feb-1571
c11-Mar-1571
c11-Apr-1571
c11-Jan-1881
c11-Feb-1891
d11-Jan-1511
d11-Feb-1521
d11-Mar-1522
d11-Apr-1532
e11-Jan-15102
e11-Feb-15111

 

Output table

IDDateGradeGroupNew gradeDate of new gradeNo. of months to get to new grade
a11-Jan-1531   
a11-Feb-153241-Apr-152
a11-Mar-1531   
a11-Apr-1542   
b11-Jan-155241-Mar-152
b11-Feb-1552   
b11-Mar-1541   
c11-Jan-157281-Jan-1836
c11-Feb-1571   
c11-Mar-1571   
c11-Apr-1571   
c11-Jan-1881   
c11-Feb-1891   
d11-Jan-1511   
d11-Feb-1521   
d11-Mar-152231-Apr-151
d11-Apr-1532   
e11-Jan-15102111-Feb-151
e11-Feb-15111   

 

8 REPLIES 8
Shilin
Calcite | Level 5

Hi, i am looking to create additional 3 columns from the input table below to give the earliest grade change if an ID falls into group 2. For example for ID = a1, it was classified as Group 2 on 1 Feb 2015 and the earliest change in grade happens on 1 Apr 2015 when it changes to 4. I would also like to calculate the number of months it took to change from grade 3 to grade 4. i have also attached the desired output table below.   Thanks!

 

Input table

IDDateGradeGroup
a1

1 jan 2015

31
a11 feb 201532
a11 mar 201531
a11 apr 201541
b1

1 jan 2015

52
b11 feb 201551
b11 mar 201551
c1

1 jan 2015

51
c11 feb 201551
c11 mar 201551
c11 apr 201551
c11 jan 201862
c11 feb 201871
d1

1 jan 2015

42
d11 jan 201651
d11 feb 201652
d11 mar 201661
e11 jan 201572
e11 jan 201781

 

Output table

IDDateGradeGroupNew gradeDate of new gradeNo. of mths to get to new grade
a1

1 jan 2015

31   
a11 feb 20153241 apr 20152
a11 mar 201531   
a11 apr 201541   
b1

1 jan 2015

52   
b11 feb 201551   
b11 mar 201551   
c1

1 jan 2015

5161 jan 201836
c11 feb 201551   
c11 mar 201551   
c11 apr 201551   
c11 jan 20186271 feb 20181
c11 feb 201871   
d1

1 jan 2015

4251 jan 201612
d11 jan 201651   
d11 feb 20164261 mar 20161
d11 mar 201661   
e11 jan 20157281 jan 201724
e11 jan 201781   
ballardw
Super User

Identical posts combined.

 

Kurt_Bremser
Super User

The first expected entry for c1 does not match your description, because it is in group 1.

Otherwise, this does what your description says:

data have;
infile datalines dlm="09"x;
input ID $ Date	:date11. Grade Group;
format date yymmdd10.;
datalines;
a1	1 jan 2015	3	1
a1	1 feb 2015	3	2
a1	1 mar 2015	3	1
a1	1 apr 2015	4	1
b1	1 jan 2015	5	2
b1	1 feb 2015	5	1
b1	1 mar 2015	5	1
c1	1 jan 2015	5	1
c1	1 feb 2015	5	1
c1	1 mar 2015	5	1
c1	1 apr 2015	5	1
c1	1 jan 2018	6	2
c1	1 feb 2018	7	1
d1	1 jan 2015	4	2
d1	1 jan 2016	5	1
d1	1 feb 2016	5	2
d1	1 mar 2016	6	1
e1	1 jan 2015	7	2
e1	1 jan 2017	8	1
;

proc sql;
create table want as
  select
    t1.id,
    t1.date,
    t1.grade,
    t1.group,
    case
      when t1.group = 2 and t2.id is not missing
      then t2.grade
      else .
    end as new_grade,
    case
      when t1.group = 2 and t2.id is not missing
      then t2.date
      else .
    end as new_date format=yymmdd10.,
    case
      when t1.group = 2 and t2.id is not missing
      then intck('month',t1.date,t2.date)
      else .
    end as diff_months
  from have t1
  left join have t2
  on t1.id = t2.id and t2.grade > t1.grade and t2.date > t1.date
  group by t1.id, t1.date, t1.grade, t1.group
  having t2.date = min(t2.date)
;
quit;

Note how the source data is presented as a data step with datalines. This allows everybody to recreate the dataset with a simple copy/paste and submit. Please do so yourself in the future, as it will speed up the process of helping you.

Shilin
Calcite | Level 5
Hi thanks. May I know what is t2 here as I only have one input table
Kurt_Bremser
Super User

See here:

left join have t2

I join the have dataset with itself, and for the second instance I use the alias t2 (the first is, of course, t1).

Shilin
Calcite | Level 5
Hi thanks much!
andreas_lds
Jade | Level 19

Are the dates sas-dates or strings?

The value of "group" seems to be ignored, when searching or a grade-change, right?

Shilin
Calcite | Level 5
I haven’t convert the dates. Supposed to be SAS dates

The value of “group” is not ignored. I would like to find the earliest grade change and the associated date for that new grade if an ID falls into group 2

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 8 replies
  • 980 views
  • 0 likes
  • 4 in conversation