Tuesday, September 10, 2013

Dealing SAS Date/time

/* Program 1: how to transfer Date, Time in character format to date/time format? */
data test;
  date='05/04/00';
  time='10:05';
run;

data new; set test;
  sasdate1=input(date, mmddyy8.);
  sasdate2=sasdate1;

  sastime1=input(time, time5.);
  sastime2=sastime1;

  datetim1=input(put(sasdate1,date7.)||':'||time,datetime16.);
  datetim2=datetim1;

  ** created sasdate2, sastime2, datetim2 for display purposes only **;
run;

proc print data=new;
  var sasdate1 sasdate2 sastime1 sastime2 datetim1 datetim2;
  format sasdate2 mmddyy8.
         sastime2 hhmm5.
         datetim2 datetime16.;
run;



/* How to deal with time variable in SAS/Graph? */
data one;
   input startime time5. count;
   if startime gt '12:00't then date='30sep92'd;
      else date='01oct92'd;
   datetime=dhms(date,hour(startime),minute(startime),
            second(startime));
   cards;
16:00 12.3
17:00 5.7
18:00 8.6
19:00 9.0
20:00 15.7
21:00 10.5
22:00 8.1
23:00 1.5
0:00  11.3
1:00  6.6
2:00  3.5
3:00  7.6
4:00  2.4
5:00  13.8
6:00  14.0
7:00  4.9
8:00  5.0
run;

proc print;
   var startime datetime;
run;

proc gplot data=one;
   plot count*datetime
        / haxis='30sep92:16:00'dt to '01oct92:08:00'dt
                by hour2;
   format datetime tod5.;
run;

How to change all derived variables to upper case?

/*************************************************************************
*If you ever need to change the derived variables name from lower case to
upper case, there is a SAS option that you can use to take care of that
instead of retyping them one by one.  Here's the option:
*************************************************************************/
options validvarname=upcase;

Rest.sas to set correct formchar for PC/Windows Format

This was quite useful before the ODS was introduced. 

/**************************************************************/
/*             SAS Programming Tips                           */
/*      Author: Chunqin Deng                                  */
/*Program File: reset.sas                                     */
/*        Date: June 25, 1999                                 */
/*     Purpose: put the reset.sas file in the beginning of    */
/*              each program. It will balance the unbalanced  */
/*              quotation mark and set correct formchar for   */
/*              PC/Windows Format. Formchar option will       */
/*              prevent to create a string character when you */
/*              paste the SAS output to other work            */
/*              editor software                               */
/**************************************************************/

*';        /* Balance the unbalanced single quotation mark */
*";        /* Balance the unbalanced double quotation mark */
run;
title;     /* Delete the existing title definition */
footnote;
options nocenter;
options nomprint nomlogic nosymbolgen;
options notes nodate nonumber ls=195
        ps=55 obs=max;
options formchar='|----|+|---+=|-/\*';  /* For PC/Windows Platform */
goptions reset=all;
=========================================
data try;
do i=1 to 2;
  do j=1 to 2;
  input x@@;
  output;
  end;
end;
datalines;
23 34 12 56
;
run;
proc freq;
tables i*j/chisq;
run;

Create a calendar using SAS PROC REPORT

This collection is from SAS website at http://support.sas.com/kb/50/099.html

The sample code below contains two sections. The top section creates a calendar that contains only the dates. The bottom section creates a calendar that includes text within date cells.

proc format;
   value dow
   1='Sun' 2='Mon' 3='Tue' 4='Wed' 5='Thu' 6='Fri' 7='Sat'
;
run;

%let year=2013;
%let beg_yr=%sysfunc(mdy(1,1,&year));
%let end_yr=%sysfunc(mdy(12,31,&year));


/************* Monthly calendar *************/

/* Create a data set containg one observation for each day in
  the time period specified above. */ 
data calendar_&year;
   format date mmddyy8. dow dow.;
   do date=&beg_yr to &end_yr;
      monthNum = month(date); 
      year = year(date); 
      dow = weekday(date);
      monthname = strip(upcase(put(date,monname9.)))||' '||put(year(date),z4.); 
      dom = input(put(date,day2.), 2.); 
      week = intnx('week',date,0,'e');
      output;
   end;
run;

/* Create a week number. */
data calendar_&year;
   set calendar_&year;
   by monthnum week;
   if first.monthnum then weeknum=0;
   if first.week then weeknum+1;
run;

options missing='' nobyline;
ods escapechar="^";

/* Create calendar output that contains only dates. */
title "&year";
proc report data=calendar_&year center nowd
     style(report)=[font=(Arial, 8pt)] 
     style(column)=[font=(Arial, 8pt)  cellheight=.5in cellwidth=.5in] 
     style(header)=[font=(Arial, 8pt) font_weight=bold]
     split='*';
   by monthnum;
   columns weeknum monthnum, (dow, dom);
   define weeknum / group order=internal noprint;
   define monthnum / across '' center order=data format=nlstrmon.;
   define dow / across order=data '' center preloadfmt format=dow.;
   define dom / analysis '' left;
run;


/************* Monthly calendar with holidays and notes *************/

/* Create a data set containg one observation for each day in
  the time period specified above. */ 
data calendar_&year(drop=fdoy);
   format date mmddyy8. dow dow.;
   do date=&beg_yr to &end_yr;
      monthnum = month(date); 
      year = year(date); 
      dow = weekday(date);
      monthname = strip(upcase(put(date,monname9.)))||' '||put(year(date),z4.); 
      dom = input(put(date,day2.), 2.); 
      week = intnx('week',date,0,'e');

      /* Get holidays */
      fdoy=mdy(1,1,&year);    
      length note $100; 
      note = ' '; 
                           
      if date = mdy(1,1,&year) 
         then note = "New Year's Day";

      if date = intnx('week.2',fdoy,(weekday(fdoy) ne 2)+2) 
         then note = "Martin Luther King Day";                                                                                
                                                                                                     
      else if date = intnx('week.2',intnx('month',fdoy,1),(weekday(intnx('month',fdoy,1)) ne 2)+2)
         then note = "President's Day"; 
                                                                                                   
      else if date = intnx('week.2',intnx('month',fdoy,4),(weekday(intnx('month',fdoy,4)) in (1,7))+4)
         then note = "Memorial Day";   

      else if date = mdy(7,4,&year) 
         then note = "Independence Day";

      else if date = intnx('week.2',intnx('month',fdoy,8),(weekday(intnx('month',fdoy,8)) ne 2))
         then note = "Labor Day"; 

      else if date = intnx('week.2',intnx('month',fdoy,9),(weekday(intnx('month',fdoy,9)) ne 2)+1)
         then note = "Columbus Day";    

      else if date = mdy(11,11,&year) 
         then note = "Veteran's Day";

      else if date = intnx('week.5',intnx('month',fdoy,10),(weekday(intnx('month',fdoy,10)) ne 5)+3) 
         then note = "Thanksgiving Day";

      else if date = mdy(12,25,&year) 
         then note = "Christmas Day";
      output;
   end;
run;

/* Create notes for personal dates. */
data mydates;
   length note $100;
   date = mdy(1,20,&year); note = "Mom's B-day"; output;
   date = mdy(5,25,&year); note = "^{style [foreground=blue] GRADUATION!}"; output;
   date = mdy(8,21,&year); note = "^{style [foreground=red] First Day of School}"; output;
run;

/* Merge the two date data sets together. */
data calendar_&year;
   merge calendar_&year mydates;
   by date;
   /* Append note text to the day of month value */
   length dom_notes $200;
   dom_notes = put(dom,8.) || "^n" || note;
run;

/* Create a week number. */
data calendar_&year;
   set calendar_&year;
   by monthnum week;
   if first.monthnum then weeknum=0;
   if first.week then weeknum+1;
run;

/* Transpose the data for the calendar with notes. */
proc transpose data=calendar_&year out=notes_calendar_&year;
   by monthnum monthname weeknum;
   id dow;
   var dom_notes;
run;

/* Create a calendar with dates and notes. */
title "#byval(monthnum) &year";
proc report data=notes_calendar_&year center nowd
     style(report)=[font=(Arial, 8pt)] 
     style(column)=[font=(Arial, 8pt) cellheight=1in cellwidth=1in] 
     style(header)=[font=(Arial, 8pt) font_weight=bold]
     split='*';
   by monthnum;
   format monthnum nlstrmon.;
   columns weeknum Sun Mon Tue Wed Thu Fri Sat;
   define weeknum / group order=internal noprint;
run;