I have a data table that stores each year, month and day value as ints:
year | month | day2009 | 1 | 1
2008 | 12 | 2
2007 | 5 | 5
I need to convert it to datetime value, because I need to use it in a datetime between operation. How could I do this?
Use the dhms() and mdy() functions:
wantedvalue = dhms(mdy(month,day2009,year),0,0,0);
Use the dhms() and mdy() functions:
wantedvalue = dhms(mdy(month,day2009,year),0,0,0);
Hi KurtBremser,
Thanks for giving reply,Actually question was wrongly posted, Below i posted question
Year Month Day
2009 01 01
2008 12 02
2007 05 05
These values are in different columns, so i want to
combine these variables to get date value in character format
(2009-01-01
2008-12-02
2001-05-05).
The leading zeros make me believe that you already have the values stored in variables of type character.
If this is so, a simple concatenation will do the trick:
newvar = year !! '-' !! month !! '-' !! day;
If this produces unwanted blanks (because the variables are defined longer thatn necessary), use the strip() function to get rid of them.
If your values are actually stored as numbers, this will create the same character value:
newvar = put(mdy(year,day,month),yymmddd10.);
The third "d" in the format specifies the separator character (dash)
SAS complains about a missing $.... format. This means that you tried to apply the numeric format YYMMDDD10. to a character variable. In such a case SAS assumes that you mistyped and adds the $ for a character format on its own. This is then not found.
SAS date and datetime formats must be applied to numeric variables that hold date (days since 1960-01-01) or datetime (seconds since 1960-01-01:00:00:00) values.
Actually i want the variable in charcter format with requried date format.
@gowtham112 wrote:
Actually i want the variable in charcter format with requried date format.
Is not possible. To apply a date/datetime format, the variable MUST be numeric.
But you can use the format to convert the value in the numeric variable to a character value, and store that in a separate character variable:
data test;
format date_num yymmddd10.;
date_num = date();
date_char = put(date_num, yymmddd10.);
date_num_raw = date_num;
run;
proc print noobs;
run;
result:
date_ date_num date_char num_raw 2016-08-19 2016-08-19 20685
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.