Hello SAS programmers,
I have the following data. Looking to count forward 12 months from birthdt (anchor date). If baby is enrolled 9 of 12 months in that first year of life, then ceyr1flag=1 (else flag is 0). Count forward another 12 months (this is year 2 of life now). If baby enrolled 9 of 12 months, then ceyr2flag=1 (else 0). While I only provide 2 years of data below to keep this post a reasonable length, I'm looking to do this through the first 5 years of life. Would have 5 flags (i.e. ceyr1flag--ceyr5flag).
Can someone help me with some code? Thank you very much! If you would like 5 years of data for each ID, I'm happy to provide.
data have;
input ID birthdt MONYY7. year month $ enrolled;
format birthdt MONYY7.;
datalines;
6816494 Feb2006 2006 Feb 1
6816494 Feb2006 2006 Mar 1
6816494 Feb2006 2006 Apr 1
6816494 Feb2006 2006 May 1
6816494 Feb2006 2006 Jun 0
6816494 Feb2006 2006 Jul 0
6816494 Feb2006 2006 Aug 0
6816494 Feb2006 2006 Sep 0
6816494 Feb2006 2006 Oct 0
6816494 Feb2006 2006 Nov 0
6816494 Feb2006 2006 Dec 0
6816494 Feb2006 2007 Jan 0
6816494 Feb2006 2007 Feb 0
6816494 Feb2006 2007 Mar 0
6816494 Feb2006 2007 Apr 0
6816494 Feb2006 2007 May 0
6816494 Feb2006 2007 Jun 0
6816494 Feb2006 2007 Jul 0
6816494 Feb2006 2007 Aug 0
6816494 Feb2006 2007 Sep 0
6816494 Feb2006 2007 Oct 0
6816494 Feb2006 2007 Nov 0
6816494 Feb2006 2007 Dec 0
6816494 Feb2006 2008 Jan 0
7298021 Aug2006 2006 Aug 1
7298021 Aug2006 2006 Sep 1
7298021 Aug2006 2006 Oct 1
7298021 Aug2006 2006 Nov 1
7298021 Aug2006 2006 Dec 1
7298021 Aug2006 2007 Jan 1
7298021 Aug2006 2007 Feb 1
7298021 Aug2006 2007 Mar 1
7298021 Aug2006 2007 Apr 1
7298021 Aug2006 2007 May 1
7298021 Aug2006 2007 Jun 1
7298021 Aug2006 2007 Jul 1
7298021 Aug2006 2007 Aug 1
7298021 Aug2006 2007 Sep 1
7298021 Aug2006 2007 Oct 1
7298021 Aug2006 2007 Nov 1
7298021 Aug2006 2007 Dec 1
7298021 Aug2006 2008 Jan 1
7298021 Aug2006 2008 Feb 1
7298021 Aug2006 2008 Mar 1
7298021 Aug2006 2008 Apr 1
7298021 Aug2006 2008 May 1
7298021 Aug2006 2008 Jun 1
7298021 Aug2006 2008 Jul 1
7358913 Nov2006 2006 Nov 1
7358913 Nov2006 2006 Dec 1
7358913 Nov2006 2007 Jan 1
7358913 Nov2006 2007 Feb 1
7358913 Nov2006 2007 Mar 1
7358913 Nov2006 2007 Apr 1
7358913 Nov2006 2007 May 1
7358913 Nov2006 2007 Jun 1
7358913 Nov2006 2007 Jul 1
7358913 Nov2006 2007 Aug 1
7358913 Nov2006 2007 Sep 1
7358913 Nov2006 2007 Oct 1
7358913 Nov2006 2007 Nov 1
7358913 Nov2006 2007 Dec 1
7358913 Nov2006 2008 Jan 1
7358913 Nov2006 2008 Feb 1
7358913 Nov2006 2008 Mar 1
7358913 Nov2006 2008 Apr 0
7358913 Nov2006 2008 May 0
7358913 Nov2006 2008 Jun 0
7358913 Nov2006 2008 Jul 0
7358913 Nov2006 2008 Aug 0
7358913 Nov2006 2008 Sep 0
7358913 Nov2006 2008 Oct 0
7487475 Dec2012 2012 Dec 1
7487475 Dec2012 2013 Jan 1
7487475 Dec2012 2013 Feb 1
7487475 Dec2012 2013 Mar 1
7487475 Dec2012 2013 Apr 1
7487475 Dec2012 2013 May 1
7487475 Dec2012 2013 Jun 1
7487475 Dec2012 2013 Jul 1
7487475 Dec2012 2013 Aug 0
7487475 Dec2012 2013 Sep 0
7487475 Dec2012 2013 Oct 0
7487475 Dec2012 2013 Nov 0
7487475 Dec2012 2013 Dec 0
7487475 Dec2012 2014 Jan 0
7487475 Dec2012 2014 Feb 0
7487475 Dec2012 2014 Mar 1
7487475 Dec2012 2014 Apr 1
7487475 Dec2012 2014 May 1
7487475 Dec2012 2014 Jun 1
7487475 Dec2012 2014 Jul 1
7487475 Dec2012 2014 Aug 1
7487475 Dec2012 2014 Sep 1
7487475 Dec2012 2014 Oct 1
7487475 Dec2012 2014 Nov 1
;
run;
You would make your life easier if your months were numbers 1 through 12 rather than character strings.
Step 1 — compute year of life
data have;
input ID birthdt MONYY7. year month $ enrolled;
currmonth=input(cats('01',month,year),date9.);
year_of_life=intck('year',birthdt,currmonth,'c');
format birthdt monyy7.;
datalines; /* I have left the data out here, just because it is long and no need to repeat it */
;
run;
Step 2 — add up all the 1s and 0s under enrollment for each year of life
proc summary data=have nway;
class id year_of_life;
var enrolled;
output out=sums sum=sum_enrolled;
run;
Step 3 — if the sum of the enrolled is 9 or more, you get the year flag=1, else 0 — I leave this up to you as a homework assignment. I will say that I strongly recommend that you leave the data in the long form as outputted from PROC SUMMARY, with one more variable if the sum of enrolled is 9 or more; rather than creating 5 new variables ceyr1flag,ceyr2flag, etc. which is a lot more programming to create, and a lot more programming to use.
You would make your life easier if your months were numbers 1 through 12 rather than character strings.
Step 1 — compute year of life
data have;
input ID birthdt MONYY7. year month $ enrolled;
currmonth=input(cats('01',month,year),date9.);
year_of_life=intck('year',birthdt,currmonth,'c');
format birthdt monyy7.;
datalines; /* I have left the data out here, just because it is long and no need to repeat it */
;
run;
Step 2 — add up all the 1s and 0s under enrollment for each year of life
proc summary data=have nway;
class id year_of_life;
var enrolled;
output out=sums sum=sum_enrolled;
run;
Step 3 — if the sum of the enrolled is 9 or more, you get the year flag=1, else 0 — I leave this up to you as a homework assignment. I will say that I strongly recommend that you leave the data in the long form as outputted from PROC SUMMARY, with one more variable if the sum of enrolled is 9 or more; rather than creating 5 new variables ceyr1flag,ceyr2flag, etc. which is a lot more programming to create, and a lot more programming to use.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.