简体   繁体   中英

sas macro do loop how many day in a year

i have data like below.

i need to show for each person how many day live in spesific year.

ı tried a lot of do loops and if statements in macro .

ı worked to much but i couldnt do so my hands empty and my heart broken.

*name  STARTDATE    ENDDATE 

*AAA    17.10.2012  21.11.2013

*BBB    10.05.2014  15.09.2015

*CCC    06.04.2010  05.05.2013

*DDD    07.02.2011  07.02.2013

*EEE    30.03.2013  30.01.2014

*FFF    01.01.2010  06.05.2010

As a result i need this

*name   STARTDATE   ENDDATE DayIn2010   DayIn2011   DayIn2012   DayIn2013   DayIn2014   DayIn2015

*AAA    17.10.2012  21.11.2013  0   0   75  325 0   0

*BBB    10.05.2014  15.09.2015  0   0   0   0   235 258

*CCC    06.04.2010  05.05.2013  269 365 365 125 0   0

*DDD    07.02.2011  07.02.2013  0   327  365    38  0   0

*EEE    30.03.2013  30.01.2014  0   0   0   276 30  0

*FFF    01.01.2010  06.05.2010  125 0   0   0   0   0

could you please help me?

Here's one way you can do this. No macros needed, just a data step do-loop:

data have;
input name $3. (STARTDATE ENDDATE) (+1 ddmmyy10.);
format STARTDATE ENDDATE ddmmyy10.;
cards;
AAA 17.10.2012 21.11.2013
BBB 10.05.2014 15.09.2015
CCC 06.04.2010 05.05.2013
DDD 07.02.2011 07.02.2013
EEE 30.03.2013 30.01.2014
FFF 01.01.2010 06.05.2010
;
run;

data want;
    set have;
    format YEAR_START YEAR_END ddmmyy10.;
    do YEAR = 2010 to 2015;
        YEAR_START = mdy(1,1,YEAR);
        YEAR_END   = mdy(12,31,YEAR);
        DAYS_ALIVE = max(0,min(ENDDATE, YEAR_END) - max(STARTDATE,YEAR_START) + 1);
        output;
    end;
run;

You would need to know in advance what the minimum and maximum year values are in your dataset so that you can write the correct do-loop. If you don't then you can write another bit of code to do that.

Here is another option that you may not need to have the knowledge of start/end year in advance.

data have;
input name $3. (STARTDATE ENDDATE) (+1 ddmmyy10.);
format STARTDATE ENDDATE ddmmyy10.;
cards;
AAA 17.10.2012 21.11.2013
BBB 10.05.2014 15.09.2015
CCC 06.04.2010 05.05.2013
DDD 07.02.2011 07.02.2013
EEE 30.03.2013 30.01.2014
FFF 01.01.2010 06.05.2010
;
run;

data s1;
set have;
do dt=startdate to enddate;
output;
end;
run;

ods output CrossTabFreqs=s2 (where=(not missing(dt) and not missing(name)) keep=name dt frequency );
proc freq data=s1;
tables name*dt /
    NOROW
        NOCOL
        NOPERCENT
        NOCUM;
format dt year4.;
run;

proc sort data=s2;
by name;
run;

PROC TRANSPOSE DATA=s2
    OUT=want(drop=_name_ _label_)
    PREFIX=DaysIn
;
    BY name;
    ID dt;
    VAR Frequency;

RUN; QUIT

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM