BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
gowtham112
Obsidian | Level 7

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?

1 ACCEPTED SOLUTION
8 REPLIES 8
gowtham112
Obsidian | Level 7

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

Kurt_Bremser
Super User

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)

gowtham112
Obsidian | Level 7
thank you for solution ..
gowtham112
Obsidian | Level 7
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.

Kurt_Bremser
Super User

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.

 

gowtham112
Obsidian | Level 7

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

Kurt_Bremser
Super User

@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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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