DATA Step, Macro, Functions and more

convert ((int)year,(int)month,(int)day) to Datetime [duplicate]

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 13
Accepted Solution

convert ((int)year,(int)month,(int)day) to Datetime [duplicate]

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?


Accepted Solutions
Solution
‎08-19-2016 05:34 AM
Super User
Posts: 6,927

Re: convert ((int)year,(int)month,(int)day) to Datetime [duplicate]

Use the dhms() and mdy() functions:

wantedvalue = dhms(mdy(month,day2009,year),0,0,0);
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Solution
‎08-19-2016 05:34 AM
Super User
Posts: 6,927

Re: convert ((int)year,(int)month,(int)day) to Datetime [duplicate]

Use the dhms() and mdy() functions:

wantedvalue = dhms(mdy(month,day2009,year),0,0,0);
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 13

Re: convert ((int)year,(int)month,(int)day) to Datetime [duplicate]

[ Edited ]

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).

Super User
Posts: 6,927

Re: convert ((int)year,(int)month,(int)day) to Datetime [duplicate]

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)

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 13

Re: convert ((int)year,(int)month,(int)day) to Datetime [duplicate]

thank you for solution ..
Occasional Contributor
Posts: 13

Re: convert ((int)year,(int)month,(int)day) to Datetime [duplicate]

i tried to solve the problem but it showing error message
format DOB yymmddd10.;
----------
48
ERROR 48-59: The format $YYMMDDD was not found or could not be loaded.

Super User
Posts: 6,927

Re: convert ((int)year,(int)month,(int)day) to Datetime [duplicate]

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.

 

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 13

Re: convert ((int)year,(int)month,(int)day) to Datetime [duplicate]

Actually i want the variable in charcter format with requried date format.

Super User
Posts: 6,927

Re: convert ((int)year,(int)month,(int)day) to Datetime [duplicate]


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 
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 363 views
  • 3 likes
  • 2 in conversation