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

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

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Mscarboncopy
Pyrite | Level 9

Thank you Astounding !! This works !

View solution in original post

6 REPLIES 6
Reeza
Super User

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

 

 

 

 


ballardw
Super User

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

Astounding
PROC Star

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

Mscarboncopy
Pyrite | Level 9

Thank you Astounding !! This works !

Tom
Super User Tom
Super User

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.

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
  • 6 replies
  • 26557 views
  • 3 likes
  • 6 in conversation