How to format dates?

Accepted Solution Solved
Reply
Highlighted
New Contributor
Posts: 2
Accepted Solution

How to format dates?

Does anyone know how I can create date variables and format them so they show up as MM/DD/YYYY ? I have read other posts and  I have not found any examples with several dates being entered in the format I have them. I also have not seen anyone creating the date variables as I need to do. I can input the dates from another file I need to assign them in statements per ID. SOmething like if ID = XX then do;

and I list all of the variables with their respective values.

All of the variables are being created in a previous statement.

 

I am creating the (formatted) var dte from the variable date (also created).

 

So I first have to create the variable date=.;

 

as a numeric variable to then format it.

 

This is in a long data statement, where I enter a lot of other numeric and character variables I need to create to be able to enter my data...and  I do have a lot of different dates to enter in the code.

 

Maybe this is my mistake ? Should I be creating the date variable as a string ?

date = " " ; ?

it would be easier for me to enter the dates if they were numeric, this is why I chose the numeric. When one is trying to create a date variable - not able to pull it out from anywhere - how is that done ?  I have never created a date variable before, so I am a little thrown off.  

 

If I create the var dte and format it before I enter the data - when I enter 04/05/2004 (one date example) The variable dte is there but it is being displayed as 01/01/1960

 

 

 

This is the code I am using to format the date variable.

 

Data test;
set new;
dte = input ( (put date, 10.), mmddyy10.);
format dte mmddyy10.;
drop date;
Run;

 

So after this step, I enter all my data (and all the other variables are fine - none need for formatting) but  dte = 04/05/2004;

is being displayed as 01/01/1960 instead.

 

If I use the input/format, after entering the data, the var dte is also created, but it is now blank. 

 

Thank you !!

M

 

 

 

 


Accepted Solutions
Solution
Thursday
New Contributor
Posts: 2

Re: How to format dates?

[ Edited ]
Posted in reply to Astounding

Thank you Astounding !! This works !

View solution in original post


All Replies
Super User
Posts: 22,818

Re: How to format dates?

Posted in reply to Mscarboncopy

What are you starting with? 

Please provide some sample data to show what you want. 

 

 


@Mscarboncopy wrote:

 

 

So I first have to create the variable date=.;

 

as a numeric variable to then format it.

 


SAS does not require you to pre-define variables so a VARIABLE statement doesn't do what you think it does and may mess up your logic.

 


@Mscarboncopy wrote:

 

If I create the var dte and format it before I enter the data - when I enter 04/05/2004 (one date example) The variable dte is there but it is being displayed as 01/01/1960

 

SAS dates need to be specified as date literals, ie "04May2004"d, exactly, not as in ddmmyy. 

 


@Mscarboncopy wrote:

 

 

This is the code I am using to format the date variable.

 

Data test;
set new;
dte = input ( (put date, 10.), mmddyy10.);
format dte mmddyy10.;
drop date;
Run;

 


We don't know what you're starting with, so there's no way to say what is correct or not. 

 

SAS dates are numbers, and represent the number of days from Jan 1, 1960. So a date of 0 is Jan 1, 1960 and 100 is April 10th, 1960. To control the appearance of a date you would use a format. This type of structure is what most programming languages and Excel use, though the 'start date' (jan 1, 1960) can differ between programs. Excel uses Jan 1, 1900 I believe. This type of structure allows you to do date calculations by subtractions and additions as necessary. 

 

data demo;
date = 0;output;
date = 100; output; format date date9.; run; proc print data=demo; run;
Spoiler

@Mscarboncopy wrote:

Does anyone know how I can create date variables and format them so they show up as MM/DD/YYYY ? I have read other posts and  I have not found any examples with several dates being entered in the format I have them. I also have not seen anyone creating the date variables as I need to do. I can input the dates from another file I need to assign them in statements per ID. SOmething like if ID = XX then do;

and I list all of the variables with their respective values.

All of the variables are being created in a previous statement.

 

I am creating the (formatted) var dte from the variable date (also created).

 

So I first have to create the variable date=.;

 

as a numeric variable to then format it.

 

This is in a long data statement, where I enter a lot of other numeric and character variables I need to create to be able to enter my data...and  I do have a lot of different dates to enter in the code.

 

Maybe this is my mistake ? Should I be creating the date variable as a string ?

date = " " ; ?

it would be easier for me to enter the dates if they were numeric, this is why I chose the numeric. When one is trying to create a date variable - not able to pull it out from anywhere - how is that done ?  I have never created a date variable before, so I am a little thrown off.  

 

If I create the var dte and format it before I enter the data - when I enter 04/05/2004 (one date example) The variable dte is there but it is being displayed as 01/01/1960

 

 

 

This is the code I am using to format the date variable.

 

Data test;
set new;
dte = input ( (put date, 10.), mmddyy10.);
format dte mmddyy10.;
drop date;
Run;

 

So after this step, I enter all my data (and all the other variables are fine - none need for formatting) but  dte = 04/05/2004;

is being displayed as 01/01/1960 instead.

 

If I use the input/format, after entering the data, the var dte is also created, but it is now blank. 

 

Thank you !!

M

 

 

 

 


Super User
Posts: 12,994

Re: How to format dates?

Posted in reply to Mscarboncopy

@Mscarboncopy wrote:

Does anyone know how I can create date variables and format them so they show up as MM/DD/YYYY ? I have read other posts and  I have not found any examples with several dates being entered in the format I have them.


So show examples of the values that you do have.

 

If by 'in the format I have them' means that you have dates in different formats on different records then perhaps you want to use the ANYDTDTE informat. But without examples it is hard to be sure.

Also I may be easier on reading the data the first time instead of "fixing" a numeric as leading zeroes and such that are important in some data formats may be missing.

Super User
Posts: 6,523

Re: How to format dates?

Posted in reply to Mscarboncopy

If you want to create a date, and give it a specific value, use this form:

 

dte = '05Apr2004'd;

 

The syntax is correct!  Don't leave anything out!

 

Then to format the variable:

 

format dte mmddyys10.;

 

The "s" indicates the type of separator to use, so "s" = "slash", "d" = "dash",  "n" = "none".  These would also be valid (although they are slightly different from one another):

 

format dte mmddyyd10.;

format dte mmddyyn8.;

Solution
Thursday
New Contributor
Posts: 2

Re: How to format dates?

[ Edited ]
Posted in reply to Astounding

Thank you Astounding !! This works !

Super User
Super User
Posts: 7,844

Re: How to format dates?

Posted in reply to Mscarboncopy

So after this step, I enter all my data (and all the other variables are fine - none need for formatting) but  dte = 04/05/2004;

is being displayed as 01/01/1960 instead.

Sounds like you tried to do something like this:

data want ;
  format dte mmddyys10. ;
  dte = 04/05/2004;
run;

So you told SAS that DTE is a numeric variable and it should apply the MMDDYYS10. format when displaying the values. You then told it to set DTE equal .8 divided by 2,004.  So approximately 0.0003992015968.  Since dates a numbers of days and you assigned a value of less than 1 that will be displayed as '01/01/1960' by the format you attached to the variable.

Super User
Posts: 9,548

Re: How to format dates?

Posted in reply to Mscarboncopy

I strongly recommend to study the relevant documentation thoroughly, starting here:

http://documentation.sas.com/?cdcId=pgmsascdc&cdcVersion=9.4_3.3&docsetId=lrcon&docsetTarget=p1wj0wt...

This page was found by entering "sas dates" in the search bar on documentation.sas.com

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
☑ This topic is solved.

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

Discussion stats
  • 6 replies
  • 160 views
  • 3 likes
  • 6 in conversation