Sas изменить формат даты
is a value that represents the number of days between January 1, 1960, and a specified date. SAS can perform calculations on dates ranging from A.D. 1582 to A.D. 19,900. Dates before January 1, 1960, are negative numbers; dates after are positive numbers.
- · SAS date values account for all leap year days, including the leap year day in the year 2000.
- · SAS date values can reliably tell you what day of the week a particular day fell on as far back as September 1752, when the calendar was adjusted by dropping several days. SAS day-of-the-week and length-of-time calculations are accurate in the future to A.D. 19,900.
- · Various SAS language elements handle SAS date values: functions, formats, and informats.
SAS time value
is a value representing the number of seconds since midnight of the current day. SAS time values are between 0 and 86400.
SAS datetime value
is a value representing the number of seconds between January 1, 1960 and an hour/minute/second within a specified date.
The following figure shows some dates written in calendar form and as SAS date values.
How SAS Converts Calendar Dates to SAS Date Values
SAS software can read two-digit or four-digit year values. If SAS encounters a two-digit year, the YEARCUTOFF= option can be used to specify which century within a 100- year span the two-digit year should be attributed to. For example, YEARCUTOFF=1950 means that two-digit years 50 through 99 correspond to 1950 through 1999, while two-digit years 00 through 49 correspond to 2000 through 2049. Note that while the default value of the YEARCUTOFF= option in SAS 9 is 1920, you can adjust the YEARCUTOFF= value in a DATA step to accommodate the range of date values you are working with at the moment. To correctly handle 2-digit years representing dates between 2000 and 2099, you should specify an appropriate YEARCUTOFF= value between 1901 and 2000. For more information, see the YEARCUTOFF= System Option in SAS Language Reference: Dictionary.
Using the YEARCUTOFF= System Option
SAS software treats the year 2000 like any other leap year. If you use two-digit year numbers for dates, you’ll probably need to adjust the default setting for the YEARCUTOFF= option to work with date ranges for your data, or switch to four-digit years. The following program changes the YEARCUTOFF= value to 1950. This change means that all two digit dates are now assumed to fall in the 100-year span from 1950 to 2049.
put ‘formatted date=’a date9.;
The PUT statement writes the following lines to the SAS log:
Note : Whenever possible, specify a year using all four digits. Most SAS date and time language elements support four digit year values.
Example : How YEARCUTOFF= Affects Two and Four-Digit Years
The following example shows what happens with data that contains both two and four-digit years. Note how the YEARCUTOFF= option is set to 1920.
options yearcutoff=1920 nodate pageno=1 linesize=80 pagesize=60;
input @1 jobid $ @6 projdate mmddyy10.;
proc print data=schedule;
format projdate mmddyy10.;
The resulting output from the PROC PRINT statement looks like this:
Output from The Previous DATA Step Showing Four-Digit Years That Result from Setting YEARCUTOFF= to 1920
The SAS System 1
Obs jobid projdate
1 A100 01/15/1925
2 A110 03/15/2025
3 A200 01/30/1996
4 B100 02/05/2000
5 B200 06/15/2000
Here are some facts to note in this example:
- In the data lines in the DATA step, the first record contains a two-digit year of 25, and the second record contains a four-digit year of 2025. Because the YEARCUTOFF= system option is set to 1920, the two-digit year defaults to a year in the 1900s in observation number 1. The four-digit year in observation number 2 is unaffected by the YEARCUTOFF= option.
- The third record is similar to the first and defaults to a year in the 1900s based on the value of YEARCUTOFF=.
- The output from records 4 and 5 shows results that are similar to records 1 and 2. The fourth record specifies a two-digit year of 00, and the fifth one specifies a four-digit year of 2000. Because of the value of the YEARCUTOFF= option, the years in the two resulting observations are the same.
As you can see, specifying a two-digit year might or might not result in the intended century prefix. The optimal value of the YEARCUTOFF= option depends on the range of the dates that you are processing.
In Releases 6.06 through 6.12 of SAS, the default value for the YEARCUTOFF= system option is 1900; starting with SAS 7, the default value is 1920.
For more information on how SAS handles dates, see the section on dates, times, and datetime values.
Practices That Help Ensure Date Integrity
The following practices help ensure that your date values are correct during all the conversions that occur during processing:
- Store dates as SAS date values, not as simple numeric or character values.
- Use the YEARCUTOFF= system option when converting two-digit dates to SAS date values.
- Examine sets of raw data coming into your SAS process to make sure that any dates containing two-digit years will be correctly interpreted by the YEARCUTOFF= system option. Look out for
- two-digit years that are distributed over more than a 100-year period. For dates covering more than a 100-year span, you must either use four digit years in the data, or use conditional logic in a DATA step to interpret them correctly.
- two-digit years that need an adjustment to the default YEARCUTOFF= range. For example, if the default value for YEARCUTOFF= in your operating environment is 1920 and you have a two-digit date in your data that represents 1919, you will have to adjust your YEARCUTOFF= value downward by a year in the SAS program that processes this value.
Note: The YEARCUTOFF= option has no effect on dates that are already stored as SAS date values.
Informats and Formats
SAS converts date, time, and datetime values back and forth between calendar dates and clock times with SAS language elements called formats and informats.
- Formats present a value, recognized by SAS, such as a time or date value, as a calendar date or clock time in a variety of lengths and notations.
- Informats read notations or a value, such as a clock time or a calendar date, which might be in a variety of lengths, and then convert the data to a SAS date, time, or datetime value.
SAS can read date and time values that are delimited by the following characters:
The blank character can also be used.
Only one delimiter can be used for a date. Otherwise, an error message is written to the SAS log. For example, 01/Jan/2007 uses a single delimiter, and can be read by SAS. In the case of, 01-Jan/2007, two different delimiters separate the parts of the date, which results in an error message.
Date and Time Tools by Task
The following table correlates tasks with various SAS language elements that are available for working with time and date data.
Dates, times, and date-times are commonly used variable types in data analysis. In SAS, dates and times are numeric variables.
- Create date, time or DateTime values
- Extract part of a date
- Computing interval between two dates
SAS stores date, time and DateTime variables as integers. This helps in calculating the differences between dates and many other numerical calculations.
SAS time values are stored as the number of seconds between midnight of the current day and another time value.SAS DateTime values are stored internally as the number of seconds between midnight, January 1, 1960, and the specified date and time.
SAS date values are stored internally as the number of days between January 1, 1960, and a specified date. Dates after January 1, 1960, are stored as positive numbers and dates before January 1, 1960, are stored as negative numbers.
Actual and Formatted SAS date values
Note the actual value stored and the formatted output in the SAS log.
Functions to create date and time values
- DATE() – It returns today’s date as a SAS date value.
- TODAY() – It returns today’s date as a SAS date value.
- MDY(m,d,y) – It returns a SAS date value from the given month (m), day (d), and year (y) values.
- DATEJUL(juldate) – converts a Julian date (juldate) to a SAS date value
- YYQ(y, q) – returns a SAS date value from the given year (y) and a quarter (q) 1, 2, 3, or 4
- TIME() – Returns the time of the day when the program was run;
- DATETIME() – Returns DateTime value for the current date and time.
- DHMS(date,hour,minutes,seconds) – Returns a SAS DateTime value for given date,hour,minutes and seconds
- HMS(h,m,s) – Returns SAS time value from hours,minutes and seconds.
The date( ) and today( ) functions returns the current date.
A Julian date is represented in the form of yyddd or yyyyddd
yy or yyyy represents the year for 2 digits or 4 digits and ddd is the number of the day of the year. The value ddd is between 001 and 365 (or 366 for a leap year). For instance, the SAS Julian date for January 11, 2020, is 2020011.
Creating SAS date, DateTime and time values
Functions to extract part of dates
- DAY(date) – The day function returns the day of the month from a given SAS date value.
- WEEKDAY(date) – To extract the day of the week from a SAS date. (1=Sunday,2=Monday and so on.)
- WEEK(date, modifiers) – To extract the week number of the year from a SAS date. The week number value is between 0-53 or 1-53 based on the modifiers(u,v,w). For details on the modifier refer to the SAS documentation.
- MONTH(date) – The month function returns the month(1-January,2-February and so on) from a SAS date.
- QTR(date) – It extracts the Quarter(January-March =1, April- June =2,etc)
- YEAR(date) – Year function returns the year from a SAS date value.
- NWKDOM(n, weekday, month, year) – Returns the date for the nth occurrence of a weekday for the specified month and year.
- n is the numeric week in the range of 1-5 of the month that contains the specified day.N=5 indicates that the specified day occurs in the last week of that month.
- weekday – It specifies week number in the range of 1- 7. Sunday is considered the first day of the week and has a weekday value of 1.
- month – specifies the month number in the range 1-12
- year – specifies a four-digit calendar year.
The date can be specified either as a variable name or as a SAS date constant. Otherwise, fairly self-explanatory! Let’s take a look at an example.
Extracting parts of SAS date values
Output:
You can use the NWKDOM function to find dates like the last Saturday in June. Use the following statement to calculate the date:
which is 27JUN2020
Functions to extract hours, minutes and seconds from SAS date-time values
- HOUR – It extracts the hour from a SAS DateTime or time value.
- MINUTE – It extracts the minutes from a SAS DateTime or time value.
- SECOND – It extracts the hour from a SAS DateTime or time value.
Extracting hours, minutes and seconds from SAS date-time values.
Output:
Using functions to calculate intervals
The functions that can be used to calculate intervals include:
- YRDIF(startdate, enddate, ‘method‘)- It returns the difference in years between two SAS date values (startdate, enddate) using one of four methods (‘method’)
- DATDIF(startdate, enddate, ‘method‘) – It returns the difference in days between two SAS date values (startdate, enddate) using one of four methods (‘method’)
YRDIF and its arguments example
Output:
Calculating a Person’s Age using YRDIF function
With the YRDIF function, you can also calculate a person’s age. To calculate a person’s age enter the start date and end date arguments followed by a third argument ‘AGE’. Age calculation will automatically take care of any leap years.
Output:
DATDIF example
- INTCK – It returns the number of time intervals (‘interval’) that occur between two dates (fromdate, to date)
- INTNX – applies multiples (increment) of a given interval (‘interval’) to a date value (date) and returns the resulting value, and hence can be used to identify past or future days, weeks, months, and so on
Learn more on INTNX and INTCK functions.
Calculating date After the specified date
Output:
Functions to extract date or time from SAS Datetime values
The DATEPART and TIMEPART function is used to extract date or time from a SAS DateTime value.
- DATEPART(date) – It returns a SAS date from a DateTime value.
- TIMEPART(date) – It extracts time part from a SAS DateTime value.
DATEPART and TIMEPART example
Output:
Functions to compute Holidays
- HOLIDAY(‘ holiday’, year) – The HOLIDAY function returns a SAS date value of a specified holiday for a specified year. The standard holiday values are mentioned found in the SAS documentation.
Holiday function example
Output:
Functions to work with timezones
- TZONENAME() – It Returns the current standard or daylight savings time, time zone name.
- TZONEOFF() – The TZONEOFF( ) function returns the time zone offset for the current time zone.
Zone ID or Time Zone Name and Timezone offset Example
Output:
For time zone names and time zone IDs, see Time Zone IDs and Time Zone Names.
Finding the difference between two time zones
Output:
We hoped this article helped you to understand Date functions that are available in SAS.
SAS format is the instruction that specifies how the value of a variable should be printed or displayed and SAS informats are the specification for how raw data should be read .
What is a SAS format?
SAS Format tells SAS how to print the variable. Formats and informats for a variable may or may not be the same for the variable.
There are many inbuilt SAS formats and informats and you can also create your own formats and informats using the PROC FORMAT procedure.
SAS Formats and informats are of three main types. These are Character, Numeric and dates.
For a complete list of built-in formats, you can refer to SAS 9.4 Formats and Informats: Reference.
Every variable in SAS will have a format – whether you assign one or you let SAS assign one automatically.
SAS informats are declared when you are reading in data or creating a new variable in a data step, whereas the format statement can be used in either a data step or a proc step:
Syntax:
$ → indicates a character format; its absence indicates a numeric format.
- w → specifies the format width that is the number of columns.
- d → It is used to specify the width of the decimal part.
SAS Format always contains a period (.) as a part of the name. Default values are used if you omit the w and the d values from the format.
The d value that you specify with SAS formats indicates the number of decimal places. The internal data values are not truncated or change with the formats.
For example, in DOLLAR10.2 , the w value of 10 specifies to print a maximum of 10 columns for the value and the d value of 2 specifies that two of these columns are for the decimal part of the value. This leaves eight columns for all the remaining characters in the value.
The remaining columns include the decimal point, the remaining numeric value, a minus sign if the value is negative, the dollar sign, and commas if any.
SAS tries to adjust the value into the space available if the format width is too narrow to represent a value. If adequate is not specified, SAS prints asterisks. In the following example, the result is x=**.
Character formats truncate values on the right. Numeric formats sometimes revert to the BESTw.d format.
If incompatible SAS formats are used, such as using a numeric format to write character values, SAS first attempts to use a comparable format of the other type. If this attempt fails, an error message that describes the problem appears in the SAS log.
When the value of d is greater than fifteen, the precision of the decimal value after the 15th decimal place might not be accurate.
Ways to Specify SAS Formats
- In a PUT statement
- With the PUT, PUTC, or PUTN functions
- With the %SYSFUNC macro function
- In a FORMAT statement in a DATA step or a PROC step
- In an ATTRIB statement in a DATA step or a PROC step.
PUT Statement
The PUT statement with a format after the variable name uses a format to write data values in a DATA step.
For example, the PUT statement with the DOLLARw.d format can be used to write the numeric value for AMOUNT as a dollar amount:
The DOLLARw.d format in the PUT statement produces this result as $1,145.32
PUT Function
PUT function is used to convert a numeric variable to a character variable that returns the resulting character value.
For example, the following statement converts the value of a numeric variable into a two-character hexadecimal representation.
The PUT function returns a value of 0A, which is assigned to the variable CHAR. The PUT function is also useful for converting a numeric value to a character value.
Using %SYSFUNC
The %SYSFUNC (or %QSYSFUNC) macro function executes SAS functions and applies the format to the result of the function outside of the DATA step.
For example, the below program writes a numeric value in a macro variable as a dollar amount.
FORMAT Statement
SAS Formats can be used with the FORMAT statement. Using a Format statement permanently associates a format with a variable . SAS uses the format to write the values of the variable that you specify.
For example, the following statement in a DATA step associates the COMMAw.d numeric format with the variables SALES1 through SALES3:
Note: If you assign formats with a FORMAT statement before a PUT statement, all leading blanks are trimmed.
Formats that are associated with variables by using a FORMAT statement behave like formats that are used with a colon (:) modifier in a subsequent PUT statement.
ATTRIB Statement
The ATTRIB statement can also be used to specify a format for one or more variables.
For example, in the following statement, the ATTRIB statement permanently applies the COMMAw.d format for the variables SALES1 through SALES3:
Permanent and Temporary SAS Formats
When you specify a format in a PUT statement, SAS uses the format to write data values during the DATA step but does not permanently apply the format with that variable.
To permanently apply a format to a variable, you can use a FORMAT statement or an ATTRIB statement in a DATA step.
Using a FORMAT statement or an ATTRIB statement in a PROC step associates a format with a variable for that PROC step, as well as for any output data sets that the procedure creates that contain formatted variables.
SAS date formats
The date formats can be used wherever formats are supported in the SAS language and the methods for applying remains the same as discussed earlier.
Built-in SAS Date formats
The table below shows how the data value corresponding with August 15, 2019 (SAS date 21776) would appear when the format is applied.
Built-in SAS time formats
The table below shows how the time value corresponding with 15 seconds after 1:14 PM (SAS time 47655) would appear when the format is applied.
Built-in SAS DateTime formats
Informat
An informat is an instruction that SAS uses to read data values into a variable. For example, the following value contains a dollar sign and commas: $1,000,000
You can remove the dollar sign ($) and commas (,) before storing the numeric value 1000000 in a variable, by applying the COMMA11. informat to this value.
Syntax :
- w specifies the informat width, which for most informats is the number of columns in the input data.
- d specifies an optional decimal scaling factor in the numeric informats. SAS divides the input data by 10 to the power of d.
Unless you explicitly define a variable first, SAS uses the informat to determine whether the variable is numeric or character. SAS also uses the informat to determine the length of character variables.
Informats always contain a period (.) as a part of the name. If you omit the w and the d values from the informat, SAS uses default values.
If the data contain decimal points, SAS ignores the d value and reads the number of decimal places that are actually in the input data.
If the informat width is too narrow to read all the columns in the input data, you might get unexpected results.
This problem frequently occurs with the date and time informats. To avoid this you must adjust the width of the informat to include blanks or special characters between the day, month, year, or time.
When a problem occurs with an informat, SAS writes a note to the SAS log and assigns a missing value to the variable.
Problems usually occur if you use an incompatible informat, such as a numeric informat to read character data, or if you specify the width of a date and time informat that causes SAS to read a special character in the last column.
Using informats
- in an INPUT statement
- with the INPUT, INPUTC, and INPUTN functions
- in an INFORMAT statement in a DATA step or a PROC step
- in an ATTRIB statement in a DATA step or a PROC step.
INPUT Statement
The INPUT statement with an informat after a variable name is the simplest way to read values into a variable. For example, the following INPUT statement uses two informats:
The $w. character informat reads values into the variable id. The commaw.d numeric informat reads values into the variable PRICE and mmddyy8. reads date informat into the dt variable.
INPUT Function
The INPUT function is used to convert a SAS character expression using a specified informat. The informat determines whether the resulting value is numeric or character.
In the above example, the INPUT function is used in combination with the w.d informat that converts the character value of TempCharacter to a numeric value and assigns the numeric value 98.6 to TemperatureNumber.
You can use the PUT function with a SAS format to convert numeric values to character values. For a complete discussion of the INPUT function, see our article on Variable Conversion in SAS.
INFORMAT Statement
The INFORMAT statement applies an informat to a variable. SAS uses the specified informat in any subsequent INPUT statement to read values into the variable.
For example, in the following statements, the INFORMAT statement associates the $3 informat with the variables id, comma10.3 with variable price and mmddyy10. for variable dt.
An informat that is associated with an INFORMAT statement behaves like an informat that you specify with a colon (:) format modifier in an INPUT statement.
Therefore, SAS uses a modified list input to read the variable so that the w value in an informat does not determine column positions or input field widths in an external file
The blanks that are embedded in input data are treated as delimiters unless you change the DLM= or DLMSTR= option in an INFILE statement.
For character informats, the w value in an informat specifies the length of the character variable for numeric informats, the w value is ignored.
If the INPUT statements use another style of input such as formatted input or column input that style of input is not used when you use the INFORMAT statement.
ATTRIB Statement
The ATTRIB statement can also associate an informat, as well as other attributes, with one or more variables. For example, in the following statements, the ATTRIB statement associates the DATEw. informat with the variables Birthdate and Interview:
If the informat is applied by using the INFORMAT= option in the ATTRIB statement it behaves like an informat that you specify with a colon (:) format modifier in an INPUT statement.
In this case, SAS uses a modified list input and reads the variables are read in the same ways as it does for the INFORMAT statement.
SAS does not permanently apply the informat to the variable unless an INFORMAT statement or an ATTRIB statement is used.
Date Informats
Sometimes we may have formatted text that represents a date and/or time information, and we wish to store this information in a SAS data set as date, time, or DateTime values.
SAS DateTime Informats
Most of the informats listed above provide some degree of flexibility in the exact formatting of the text they read.
For example, the TIME. informat will work with colons separating the hours, minutes, and seconds, but will also work with periods, slashes, dashes, and other characters.
It also works irrespective of “PM” is in uppercase or lowercase.
For more flexibility, SAS also includes a set of three “ANY” informats that will correctly interpret a wide variety of formatted date and time information.
I have an input file, where my dates don't have leading zeros (like 25.3.2016) but I would like to transform them into format DDMMYYYYP10.
Is there any format, informat, function etc. that could do that for me?
I'm using SAS Enterprise Guide 4.3.
3 Answers 3
- A proper informat (in your case, the ANYDTDTE10. should do) for SAS to adequately recognize the dates upon reading the data
- An output format (you are asking for DDMMYYP10. ) to display dates, given they are imported correctly with the informat above.
Of course you'll be needing an INFILE statement rather than a DATALINES if you are reading external data (which I assume is your case), but the results will be the same.
Remember that output formats are only formats. You can change them at will without affecting the underlying data. So the key here is really the informat.
My input is dataset, so this worked for me:
So in that case your Date variable did not actually have a numeric SAS date. It was a character variable with a text value, and you are using the INPUT function to convert that text value into a numeric date value.
SAS is designed to store a date as a "SAS Date", which is a numeric variable that is the number of days since Jan 1, 1960. Assuming you have a SAS date (and not a character variable that looks like a date), it should be straight forward to change the format used for this variable. Note that this doesn't actually transform the value, it just changes the format used to display the value when it is printed etc.
Edit: I reread your question and realized maybe you do not have a SAS dataset as input but instead have a text file? If so, you can read dates like 25.3.2016 using the ddmmyy10 informat. Below uses ddmmyy10 informat to read in the value from text file, and then ddmmyyp10 format to format it with period separators :
Not the answer you're looking for? Browse other questions tagged date sas leading-zero or ask your own question.
Related
Hot Network Questions
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
Site design / logo © 2022 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2022.5.11.42086
IN SAS даты являются частным случаем числовых значений. Каждому дню присваивается определенное числовое значение, начиная с 1 января 1960 года. Этой дате присваивается значение даты 0, а следующей дате присваивается значение даты 1 и т. Д. Предыдущие дни до этой даты представлены -1, -2 и так далее. При таком подходе SAS может представлять любую дату в будущем и любую дату в прошлом.
Когда SAS читает данные из источника, он преобразует прочитанные данные в определенный формат даты, как указано в формате даты. Переменная для хранения значения даты объявляется с необходимой информацией. Дата вывода отображается с использованием форматов выходных данных.
SAS Date Informat
Исходные данные могут быть прочитаны надлежащим образом с использованием определенных данных даты, как показано ниже. Цифра в конце информата указывает минимальную ширину строки даты, которая должна быть полностью прочитана с использованием информата. Меньшая ширина даст неверный результат. в SAS V9 существует общий формат даты anydtdte15. который может обрабатывать любой ввод даты.
Дата ввода Ширина даты Informat 03/11/2014 10 mmddyy10. 03/11/14 8 mmddyy8. 11 декабря 2012 г. 20 worddate20. 14mar2011 9 date9. 14-мар-2011 11 date11. 14-мар-2011 15 anydtdte15. пример
Код ниже показывает чтение различных форматов даты. Обратите внимание, что все выходные значения являются просто числами, так как мы не применили никакого оператора формата к выходным значениям.
Когда приведенный выше код выполняется, мы получаем следующий вывод.
Формат вывода даты SAS
Даты после прочтения могут быть преобразованы в другой формат в соответствии с требованиями дисплея. Это достигается с помощью оператора формата для типов даты. Они принимают те же форматы, что и информаторы.
пример
В приведенном ниже примере дата читается в одном формате, но отображается в другом формате.
Читайте также: