1# jiangh_fox
I give you some example in my real project :
The Character version of Date I`m usually encountered with looks like this "mm/dd/yyyy". What I want is to transfer this into Date9. format which could be esaily computed in SAS.
Define the Format first:
- proc format ;
- value $mon
- "01" = "JAN"
- "02" = "FEB"
- "03" = "MAR"
- "04" = "APR"
- "05" = "MAY"
- "06" = "JUN"
- "07" = "JUL"
- "08" = "AUG"
- "09" = "SEP"
- "10" = "OCT"
- "11" = "NOV"
- "12" = "DEC";
- run;
复制代码
Set up the utility macro like this :
- ** This macro used for original character dates like xx/xx/xxxx ***;
- ** Define Length dd $2. mm $3. yy $4. at first ***;
- %global dt;
- %macro dt (indt = , outdt = );
- format &outdt date9.;
- if &indt ne '' then do;
- mm = put(substr(compress(&indt), 1,2), $mon.);
- dd = substr(compress(&indt), 4,2);
- yy = substr(compress(&indt), 7,4);
- &outdt = input(dd||mm||yy, date9.);
- end;
- else do;
- &outdt = .;
- end;
- %mend dt;
复制代码
After that, you could easily call macro %dt within the rest of your program.
- ** Calculate Age from DEMO **;
- data demo_ (keep = subjid INVID ageyr SEX_LONG_NM SUBRACE_24_LONG_NM rename = (SUBRACE_24_LONG_NM = race)) ;
- length dd $2. mm $3. yy $4. biryear $10.;
- merge demo (in=a) ic (in=b);
- by subjid;
- if a ;
- biryear = "01/07/"||substr(BIRTHDT_STR, 9, 4);
- %dt(indt = ICDTTM_DT, outdt = icdt);
- %dt(indt = biryear, outdt = birthdt);
- ageyr = floor((intck('month',BIRTHDT,ICDT) - (day(ICDT) < day(BIRTHDT)))/12);
- run;
复制代码
The date transfer issue come acrossed a lot in the clinical trial projects. I hope the above contents are making sense to you.