I have data on about 30000 records, each record can have up to 17 release dates (release1-release17) and up to 17 codes decribing the release type (street1-street17- for whether or not the immate was released to the streets).
So now I want to create 60 new variables: Month1-Month60 covering the 60 months of the data of Jan 2011-Dec 2015, where Month1 is January 2011 and Month60 is December 2015. If the variable street1-street17 is Y that means if was a "true" release and this new variable (MonthY) will be the difference in days for that month and a predefined date until the next release. If multiple release dates occur on the same month, then I need to concatenate these differences. Only 5 release dates per month will be concatenated. Dates are in this format mm/dd/yyy and the predefined date is 12/28/2035.
I have 2 problems with my code- the second release is being put on the wrong month (for example it should be month33 but it is showing on Month 34). And the other problem is that it is not concatenating multiple releases on the same month.
any help will be appreciated, thanks,
malena
here is my code:
*Create a record for each month based on release date;
data have_monthly; length cat $24.; length cat2 $24.;
set have_flipped;
N_interval = intck('month', date_type, date_release);
/*If NOT MISSING(date_admission) then do;*/
if N_interval <> . then do;
Month=intnx('month', date_release, i-1, 'b'); Status=1; cat=catx(',',cat,date_type); cat2=compress(cat, ',');
if i=(N_interval+1) and release_type='Y' then status=0;
output; end;
format month yymon7.;
keep studyID month status cat2; run;
/*This section of code creates the empty data set required for the final data structure one record per month, per CHIA_random_ID */
*Create months; data empty_months; start = '01Dec2010'd; do i=1 to 60; Month=intnx('month', start, i, 'b'); Monthly_Number = catt("Month", put(i, z2.)); output; end; format month yymon7.; keep month Monthly_Number; run;
*Get list of IDs; proc sql; create table ids as select distinct id from have; quit;
*Create monthly by ID table with no status; proc sql; create table empty_status as select a.id, b.* from empty_months as b cross join ids as a order by 1, 2; quit;
*Add in status from previous calculation; proc sql; create table status as select a.*, b.cat2 from empty_status as a left join have_monthly as b on a.id=b.id and a.month=b.month order by 1,2; quit;
*fill down values so values are retained across time; data status_filled; length status 8.; set status; by id; retain final_status; if first.id then final_status=status; else if not missing(status) then final_status=status; status=final_status;
if missing (status) then status=0; drop final_status; run;
proc sort data=status_filled out= status_filled2 nodupkey equals; by id monthly_number; run;
*Transpose back to final wide data structure; proc transpose data=status_Filled2 out=want(drop= _name_); by id; var cat2; id monthly_number; idlabel month; run;
proc sort data=incarcerated out= incarcerated2 nodupkey equals; by id; run;
data data_final; set want; DOC1=Month01; DOC2=Month02; DOC3=Month03; DOC4=Month04; DOC5=Month05; DOC6=Month06; DOC7=Month07; DOC8=Month08; DOC9=Month09; DOC10=Month10; DOC11=Month11; DOC12=Month12; DOC13=Month13; DOC14=Month14; DOC15=Month15; DOC16=Month16; DOC17=Month17; DOC18=Month18; DOC19=Month19; DOC20=Month20; DOC21=Month21; DOC22=Month22; DOC23=Month23; DOC24=Month24; DOC25=Month25; DOC26=Month26; DOC27=Month27; DOC28=Month28; DOC29=Month29; DOC30=Month30; DOC31=Month31; DOC32=Month32; DOC33=Month33; DOC34=Month34; DOC35=Month35; DOC36=Month36; DOC37=Month37; DOC38=Month38; DOC39=Month39; DOC40=Month40; DOC41=Month41; DOC42=Month42; DOC43=Month43; DOC44=Month44; DOC45=Month45; DOC46=Month46; DOC47=Month47; DOC48=Month48; DOC49=Month49; DOC50=Month50; DOC51=Month51; DOC52=Month52; DOC53=Month53; DOC54=Month54; DOC55=Month55; DOC56=Month56; DOC57=Month57; DOC58=Month58; DOC59=Month59; DOC60=Month60;
run;
data Data_final; merge incarcerated2 DOC_final; by studyid; keep id gender age race DOC1-DOC60 times; run;
... View more