0% found this document useful (0 votes)
42 views3 pages

SAS Chapter 06

This document discusses various examples of working with SAS dates, including reading and writing dates in different formats, creating SAS dates from month, day and year values, computing ages from dates of birth, extracting components like day of week and month from dates, counting intervals between dates, and more. Key functions discussed are MDY, YEAR, MONTH, DAY, WEEKDAY, INTCK, INTNX and TODAY for date arithmetic, extraction and comparison. Various formats like MMDDYY, WORDDATE and DATE are also demonstrated.

Uploaded by

mihirhota
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views3 pages

SAS Chapter 06

This document discusses various examples of working with SAS dates, including reading and writing dates in different formats, creating SAS dates from month, day and year values, computing ages from dates of birth, extracting components like day of week and month from dates, counting intervals between dates, and more. Key functions discussed are MDY, YEAR, MONTH, DAY, WEEKDAY, INTCK, INTNX and TODAY for date arithmetic, extraction and comparison. Various formats like MMDDYY, WORDDATE and DATE are also demonstrated.

Uploaded by

mihirhota
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

SAS Programming by Example [6]

Chapter 6 SAS DATES Reading, Writing, and Arithmetic with Data Values
Example 1 Reading a Date from Raw Data Features: SAS Dates, SAS Date Formats and in formats Date INPUT Statement -------------------------------------------------02/23/72 INPUT @8 DATE MMDDYY8; 02-23-72 INPUT @8 DATE MMDDYY8; 02+23, 72 INPUT @8 DATE MMDDYY8; 23FEB72 INPUT @8 DATE DATE7; 022372 INPUT @8 DATE MMDDYY6; 02/23/1972 INPUT @8 DATE MMDDYY10; 23/02/72 INPUT @8 DATE DDMMYY8; 72054 (Julian date) INPUT @8 DATE JULIAN5; 1972054 INPUT @8 DATE JULIAN7; DATA PATIENT; INFILE 'HOSP'; INPUT @1 ID #2. @5 ADMITS MMDDYY8. @15 DISCHRG MMDDYY8. @25 COST 5; LOS=DISCHRG-ADMIT+1; LABEL ADMIT ='Admission Date' DISCHRG='Discharge Date' COST ='Cost of Treatment' LOS ='Length of Stay'; FORMAT ADMITS DISCHRG MMDDYY8. COST DOLLAR8; RUN; Example 2 Creating a SAS Date from Month, Day, and Year Features: MDY Function, WORDDATE Format DATA MDYEXAMP; INPUT DAY 1-2 MONTH 10-11 YEAR 20-23; DATE=MDY (MONTH, DAY, YEAR); FORMAT DATE WORDDATE; DATALINES; 12 11 1992 11 09 1899 ;

PROC PRINT DATA=MDYEXAMP; TITLE 'Example of MDY Function'; RUN; Example 3 Computing Age Features: Date Literals, TODAY function DATA AGE; SET EMPLOYEE; AGE1=INT (('01JAN95'D-DOB)/365.25); AGE2=ROUND (('01JAN95'D-DOB)/365.25, 1); AGE3=INT ((TODAY ()-DOB)/365.25); AGE4=ROUND ((TODAY ()-DOB)/365.25, 1); RUN; Example 4 Extracting Day of Week and Day of Month from a SAS Date Features: DAY and WEEKDAY Functions Note: DAY function returns 1-31 WEEKDAY returns 1-7 PROC FORMAT; VALUE DATFMT 1='SUN' 2='MON' 3='TUE' 4='WED' 5='THU' 6='FRI' 7='SAT'; RUN; DATA PATIENT; INFILE 'HOSP'; INPUT @1 ID #2. @5 ADMITS MMDDYY8. @15 DISCHRG MMDDYY8. @25 COST 5; DAY_WEEK=WEEKDAY (ADMIT); DAY_MON =DAY (ADMIT); LABEL ADMIT ='Admission Date' DISCHRG='Discharge Date' COST ='Cost of Treatment' LOS ='Length of Stay'; FORMAT ADMITS DISCHRG MMDDYY8. COST DOLLAR8. DAY_WEEK DAYFMT; RUN; PROC CHART DATA=PATIENT; VBAR DAY_WEEK DAY_MON/DISCRETE; RUN; Example 5 Extracting Month and year from a SAS Date Features: YEAR and MONTH Functions, PROC CHART (with the

SUMVAR and DISCRETE Options) DATA TEMP; SET FUND; YEAR=YEAR (DATE); MONTH=MONTH (DATE); RUN; PROC CHART DATA=TEMP; VBAR YEAR/SUMVAR=AMOUNT DISCRETE; VBAR MONTH/SUMVAR=AMOUNT DISCRETE; RUN; Example 6 Counting the Number of Years, Months, and so on, from a Given Date Feature: INTCK and TODAY Functions DATA NEW_EMP; SET EMPLOY; CURRENT=TODAY (); WORK_YES=INTCK ('YEAR', DATEHIRE, CURRENT); RUN; Note: INTCK ('YEAR'/MONTH,) returns # of a yearly/monthly Boundary has passed. Example 7 Computing Exact Age in Years Features: INTCK Function, Combining Numeric and Logical Operations AGE=INT ((INTCK ('MONTH', BIRTH, CURRENT)(DAY (CURRENT) < value. date base a after intervals of number given is which value returns Note: 10); VISIT, FOLLOWUP="INTNX ('MONTH'," Function INTNX Feature: Intervals Number Date the Computing 8 Example 12) ;>

You might also like