BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
adschiltz
Calcite | Level 5

Having some programming issues. I would like to take a table that has information populated for the first ID, and populate the rest of the observations for the ID according to specific conditions. 

 

1. I would like to populate REV_DATE_LAST with the most recent REV_DATE_LAST by ID, unless the prior observation included Mark=1.

2. I would then like to calculate REV_AGE as YEARMO - REV_DATE_LAST. 

3. Mark should =1 if REV_AGE is GE 12 unless FLAG=1

 

The intent is to review each ID every 12 months, unless they are flagged, then the review should take place once they are no longer flagged.

 

I have tried many different potential solutions with no avail including lags, retains, counts, etc.

 

HAVE:

IDYEARMOREV_DATE_LASTFLAGREV_AGEMARK
1201803201608 221
1201804    
1201805    
1201806    
1201807    
1201808    
1201809    
1201810    
1201811    
1201812    
1201901    
1201902    
1201903    
1201904    
1201905    
1201906    
2201803201607121 
2201804 1  
2201805 1  
2201806 1  
2201807 1  
2201808 1  
2201809 1  
2201810 1  
2201811 1  
2201812 1  
2201901 1  
2201902 1  
2201903    
2201904    
2201905    
2201906 1  
2201907 1  
2201908 1  
2201909 1  
2201910 1  
2201911 1  
2201912 1  
2202001 1  
2202002 1  
2202003    
2202004    
2202005    

 

WANT:

IDYEARMOREV_DATE_LASTFLAGREV_AGEMARK
1201803201608 221
1201804201804 0.
1201805201804 1.
1201806201804 2.
1201807201804 3.
1201808201804 4.
1201809201804 5.
1201810201804 6.
1201811201804 7.
1201812201804 8.
1201901201804 9.
1201902201804 10.
1201903201804 11.
1201904201804 121
1201905201905 0.
1201906201905 1.
2201803201607121.
2201804201607122.
2201805201607123.
2201806201607124.
2201807201607125.
2201808201607126.
2201809201607127.
2201810201607128.
2201811201607129.
2201812201607130.
2201901201607131.
2201902201607132.
2201903201607 331
2201904201904 0.
2201905201904 1.
220190620190412.
220190720190413.
220190820190414.
220190920190415.
220191020190416.
220191120190417.
220191220190418.
220200120190419.
2202002201904110.
2202003201904 11.
2202004201904 121
2202005202005 0 
1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Your initial rev_age values for both groups are wrong, given the values of yearmo and rev_date_last.

But see this code:

data have;
infile datalines dlm="09"x dsd truncover;
input ID YEARMO :yymmn6. REV_DATE_LAST :yymmn6. FLAG REV_AGE MARK;
format yearmo rev_date_last yymmn6.;
datalines;
1	201803	201608	 	22	1
1	201804	 	 	 	 
1	201805	 	 	 	 
1	201806	 	 	 	 
1	201807	 	 	 	 
1	201808	 	 	 	 
1	201809	 	 	 	 
1	201810	 	 	 	 
1	201811	 	 	 	 
1	201812	 	 	 	 
1	201901	 	 	 	 
1	201902	 	 	 	 
1	201903	 	 	 	 
1	201904	 	 	 	 
1	201905	 	 	 	 
1	201906	 	 	 	 
2	201803	201607	1	21	 
2	201804	 	1	 	 
2	201805	 	1	 	 
2	201806	 	1	 	 
2	201807	 	1	 	 
2	201808	 	1	 	 
2	201809	 	1	 	 
2	201810	 	1	 	 
2	201811	 	1	 	 
2	201812	 	1	 	 
2	201901	 	1	 	 
2	201902	 	1	 	 
2	201903	 	 	 	 
2	201904	 	 	 	 
2	201905	 	 	 	 
2	201906	 	1	 	 
2	201907	 	1	 	 
2	201908	 	1	 	 
2	201909	 	1	 	 
2	201910	 	1	 	 
2	201911	 	1	 	 
2	201912	 	1	 	 
2	202001	 	1	 	 
2	202002	 	1	 	 
2	202003	 	 	 	 
2	202004	 	 	 	 
2	202005	 	 	 	 
;

data want;
set have (rename=(rev_date_last=_rev_date_last mark=_mark));
by id;
retain rev_date_last mark;
format rev_date_last yymmn6.;
if first.id
then do;
  rev_date_last = _rev_date_last;
  mark = _mark;
end;
else do;
  if mark 
  then do;
    rev_date_last = yearmo;
    mark = .;
  end;
end;
rev_age = intck('month',rev_date_last,yearmo);
if not flag and rev_age ge 12 then mark = 1;
drop _:;
run;

View solution in original post

2 REPLIES 2
Kurt_Bremser
Super User

Your initial rev_age values for both groups are wrong, given the values of yearmo and rev_date_last.

But see this code:

data have;
infile datalines dlm="09"x dsd truncover;
input ID YEARMO :yymmn6. REV_DATE_LAST :yymmn6. FLAG REV_AGE MARK;
format yearmo rev_date_last yymmn6.;
datalines;
1	201803	201608	 	22	1
1	201804	 	 	 	 
1	201805	 	 	 	 
1	201806	 	 	 	 
1	201807	 	 	 	 
1	201808	 	 	 	 
1	201809	 	 	 	 
1	201810	 	 	 	 
1	201811	 	 	 	 
1	201812	 	 	 	 
1	201901	 	 	 	 
1	201902	 	 	 	 
1	201903	 	 	 	 
1	201904	 	 	 	 
1	201905	 	 	 	 
1	201906	 	 	 	 
2	201803	201607	1	21	 
2	201804	 	1	 	 
2	201805	 	1	 	 
2	201806	 	1	 	 
2	201807	 	1	 	 
2	201808	 	1	 	 
2	201809	 	1	 	 
2	201810	 	1	 	 
2	201811	 	1	 	 
2	201812	 	1	 	 
2	201901	 	1	 	 
2	201902	 	1	 	 
2	201903	 	 	 	 
2	201904	 	 	 	 
2	201905	 	 	 	 
2	201906	 	1	 	 
2	201907	 	1	 	 
2	201908	 	1	 	 
2	201909	 	1	 	 
2	201910	 	1	 	 
2	201911	 	1	 	 
2	201912	 	1	 	 
2	202001	 	1	 	 
2	202002	 	1	 	 
2	202003	 	 	 	 
2	202004	 	 	 	 
2	202005	 	 	 	 
;

data want;
set have (rename=(rev_date_last=_rev_date_last mark=_mark));
by id;
retain rev_date_last mark;
format rev_date_last yymmn6.;
if first.id
then do;
  rev_date_last = _rev_date_last;
  mark = _mark;
end;
else do;
  if mark 
  then do;
    rev_date_last = yearmo;
    mark = .;
  end;
end;
rev_age = intck('month',rev_date_last,yearmo);
if not flag and rev_age ge 12 then mark = 1;
drop _:;
run;
adschiltz
Calcite | Level 5

Kurt,

Brilliant solution. Thank you for posting! Have a great weekend.

 

- adschiltz

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 269 views
  • 1 like
  • 2 in conversation