这篇文章给大家介绍sas怎么用于时间的函数,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
处理股票数据,经常要对时间变量作处理,特意摘录sas.support.com的样例来学习sas的用于时间的函数.另外《DATE HANDLING IN THE SAS ® SYSTEM》一文中也有很多详细的例子。
*计算年龄; /* Create sample data */ data birth; input name $ bday :mmddyy10.; datalines; Miguel 12/31/1973 Joe 02/28/1976 Rutger 03/29/1976 Broguen 03/01/1976 Susan 12/12/1976 Michael 02/14/1971 LeCe 11/09/1967 Hans 07/02/1955 Lou 07/30/1960 ;run; /* Use the INTCK function to count the number of months between */ /* the date of birth and the current date. Divide the number of */ /* months by 12 to produce the number of years. Use the MONTH */ /* function to determine if the month of the birthday and the current */ /* date are the same. If they are, determine if the birthday has */ /* occurred this year. If it hasn't, adjust the age by subtracting */ /* one year. */ data ages; set birth; retain current; if _n_=1 then current=today(); format bday current worddate20.; age=int(intck('month',bday,current)/12); if month(bday)=month(current) then age=age-(day(bday)>day(current)); run; proc print;run; *计算两个日期间的年月日数目; data a; input @1 dob mmddyy10.; /* Get the current date from operating system */ tod=today(); /* Determine number of days in the month prior to current month */ bdays=day(intnx('month',tod,0)-1); /* Find difference in days, months, and years between */ /* start and end dates */ dd=day(tod)-day(dob); mm=month(tod)-month(dob); yy=year(tod)-year(dob); /* If the difference in days is a negative value, add the number */ /* of days in the previous month and reduce the number of months */ /* by 1. */ if dd < 0 then do; dd=bdays+dd; mm=mm-1; end; /* If the difference in months is a negative number add 12 */ /* to the month count and reduce year count by 1. */ if mm < 0 then do; mm=mm+12; yy=yy-1; end; format dob tod mmddyy10.; datalines; 01/01/1970 02/28/1992 01/01/2000 02/28/2000 02/29/2000 03/01/2000 05/10/1990 05/11/1990 05/12/1990 ; proc print label; label dob='Date of Birth' tod="Today's Date" dd='Difference in Days' mm= 'Difference in Months' yy='Difference in Years'; var dob tod yy mm dd; run; /*********************************************************************/ /* The following code calculates the start and end dates for */ /* daylight saving time for the years 2006 through 2009. */ /* */ /* FDOY represents the first day of the year */ /* FDO_APR represents the first day of April */ /* DST_BEG represents the first day of daylight saving time */ /* */ /* It is calculated using this syntax: */ /* intnx('week.1',fdo_apr,(weekday(fdo_apr) ne 1)) */ /* */ /* Explanation of the above syntax: */ /* - weekday(fdo_apr) returns a number between 1 and 7, */ /* which represents the day of the week, 1=Sunday and 7=Saturday */ /* */ /* - weekday(fdo_apr) ne 1) returns a 0 if the first day */ /* of April is a Sunday and 1 otherwise. */ /* */ /* - intnx('week.1',fdo_apr,(weekday(fdo_apr) ne 1)) returns a */ /* SAS date which is 0 WEEK intervals from the FDO_APR, if the */ /* first day of April is a Sunday. The WEEK.1 interval specifies */ /* Sunday as the first day of the week. */ /* */ /* OR */ /* */ /* returns a SAS date which is 1 WEEK interval from the FDO_APR, */ /* if the first day of April is not a Sunday, (using Sunday as */ /* the first day of the week). */ /* */ /* */ /* DST_END represents the end of daylight saving time and is */ /* calculated similarly to DST_BEG. */ /*********************************************************************/ data _null_; do year=2006 to 2009; fdoy=mdy(1,1,year); /* For years prior to 2007, daylight time begins in the United States on */ /* the first Sunday in April and ends on the last Sunday in October. */ if year <= 2006 then do; fdo_apr=intnx('month',fdoy,3); dst_beg=intnx('week.1',fdo_apr,(weekday(fdo_apr) ne 1)); fdo_oct=intnx('month',fdoy,9); dst_end=intnx('week.1',fdo_oct,(weekday(fdo_oct) in (6,7))+4); end; /* Due to the Energy Policy Act of 2005, Pub. L. no. 109-58, 119 Stat 594 */ /* (2005). Starting in March 2007, daylight time in the United States */ /* will begin on the second Sunday in March and end on the first Sunday */ /* in November. For more information, one reference is */ /* http://aa.usno.navy.mil */ else do; fdo_mar=intnx('month',fdoy,2); dst_beg=intnx('week.1',fdo_mar, (weekday(fdo_mar) in (2,3,4,5,6,7))+1); fdo_nov=intnx('month',fdoy,10); dst_end=intnx('week.1',fdo_nov,(weekday(fdo_nov) ne 1)); end; put dst_beg= worddate. / dst_end= worddate. / ; end; run; /* Sample 1: Variables for date, hour, minute and seconds */ data datetime; if _n_=1 then sdate=date(); retain sdate; format sasdt dateampm22. bid dollar6.0; input hr min sec bid dollar6.; sasdt=dhms(sdate,hr,min,sec); keep sasdt bid ; datalines; 8 00 25 $200 8 10 14 $300 9 24 30 $400 ; run; /* Sample 2 : Variables for date and time */ data datetime; if _n_=1 then sdate=date(); retain sdate; format sasdt dateampm22. bid dollar6.0; input stime time8. +1 bid dollar6.; /* Use zero placeholders for the H and M parameters */ sasdt=dhms(sdate,0,0,stime); keep sasdt bid ; datalines; 10:30:30 $450 11:49:20 $465 13:44:12 $475 ;run; data ds1; do date='01nov2010'd to '31dec2010'd; output; end; run; /* The WEEK variable returns a value of 1 to 5 based on the number of weeks in a month using a combination of the INTNX and INTCK functions. Note: The INTCK function returns the integer number of time intervals in a given time span. By default, Sunday is the beginning of the week interval. The INTNX function increments (either forwards or backwards) a date, time or datetime value by a specified interval. */ data ds2; set ds1; week=intck('week',intnx('month',date,0),date)+1; run; proc print; format date date9.; run; *finding the first business day of the mounth; /* Create sample data */ data test; input date :mmddyy6.; format date date9.; datalines; 010106 010406 041806 081806 123106 ; data getweek; set test; /* Use INTNX to roll DATE back to the first of the year. */ /* Pass the result as the 'start' parameter to INTCK. */ week=intck('week',intnx('year',date,0),date)+1; /* If you are in SAS 9.1, you can use the WEEK function. */ /* The WEEK function has an optional second argument that */ /* can be used to specify whether a week starts on Sunday */ /* or Monday. For more information, please refer to the */ /* SAS NLS User's Guide. */ /* U indicates Sunday is the first day of the week */ week_function=week(date,'u'); run; /* create sample data */ data one; input date mmddyy6.; format date date9.; cards; 011509 021509 030409 ; data two; set one; /* advance date to the first day of the month using the INTNX function */ first=intnx('month',date,0); /* determine the day of the week using the WEEKDAY function */ day=weekday(first); /* if day=Sunday then advance by 1 */ if day=1 then first+1; /* if day=Saturday then advance by 2 */ else if day=7 then first+2; format first date9.; proc print; title 'First Business Day of the Month'; run; /* Sample 1 -- Rolling dates forwards and backwards by month using the SAMEDAY */ /* parameter new in SAS 9.1. Do NOT 'roll over' to next month if */ /* the adjusted month has fewer days than the day value of the */ /* starting month. */ data _null_; /* Test with non-leap year */ do date='27MAR2003'd to '01APR2003'd; lastmonth= intnx('month',date,-1,'sameday'); put (date lastmonth)(=worddate.); end; /* Test with leap year */ put; do date='27MAR2004'd to '01APR2004'd; lastmonth=intnx('month',date,-1,'sameday'); put (date lastmonth)(=worddate.); end; put; /* Test future dates */ do date='27JAN2004'd to '01FEB2004'd; nextmonth=intnx('month',date,1,'sameday'); put (date nextmonth)(=worddate.); end; run; /* Sample 2 -- Rolling dates forwards and backwards by month using the SAMEDAY */ /* parameter new in SAS 9.1 while allowing shifted months with */ /* fewer days to 'roll over' to the next month. */ /* */ /* The default alignment of the INTNX function is the beginning of the shift */ /* period, the first of the month in this case. If the resulting date is in a */ /* month with less than 31 days, adjust the appropriate number of days into */ /* the next month. */ data _null_; /* Test with non-leap year */ do date='27MAR2003'd to '01APR2003'd; lastmonth=date - intnx('month',date,0)+intnx('month',date,-1); put (date lastmonth)(=worddate.); end; /* Test with leap year */ put; do date='27MAR2004'd to '01APR2004'd; lastmonth=date - intnx('month',date,0)+intnx('month',date,-1); put (date lastmonth)(=worddate.); end; put; /* Test future dates */ do date='27JAN2004'd to '01FEB2004'd; nextmonth=date - intnx('month',date,0)+intnx('month',date,1); put (date nextmonth)(=worddate.); end; run
关于sas怎么用于时间的函数就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。