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

We are trying to change the date format from mm/dd/yyyy to dd/mm/yyyy with the months written with the three first letters of each month. Do you have an idea ? 

 

Subject:  

 

The following observations are data from four mutuals funds. The data is delimited by spaces between the variables: fund ID, Fund Inception Date, Fund End Date, and volume of transactions in dollars.  10/21/2005 12/12/2010  03/29/2009 11/11/2010 

1) Create a SAS dataset that reads in the data. Call the variables ID, FID, FED, and volume. Read it from the file mutualfunds.txt. Use a format statement for the date variables. Display them as this form : "12SEP1989".

 

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

That's because you're having date as a character variable. It needs to be a numeric variable, with a date format. Then you can change the format. This is one of the reasons to not store it in a character variable. 

 

Assuming that's what you start with, you first need to convert it using INPUT() and then apply the new format. 

 

data want;
set mutualfunds;

FID_date = input(FID, mmddyy10.);
FED_date = input(FED, mmddyy10.);
format FID_Date FED_Date date9.;

run;

@AntoineMaurin wrote:

We are not able to do it on your way. Maybe it is because we have the values in a table. Do you have an idea for doing it with this table ? 

data mutualfunds;

input ID $ FID $ FED $ Volume;
cards;
001 10/21/2005 12/12/2010 850000
002 05/01/2011 07/08/2015 1200000
003 01/01/2007 01/01/2014 19512000
004 06/23/2009 11/11/2016 15805123
005 10/21/2005 12/12/2010 815000
006 05/05/2010 07/08/2015 160000
007 01/15/2004 01/01/2014 19420000
008 03/29/2009 11/11/2010 1580123
;run;


Editor's note: see also this SAS Date Functions FAQ tutorial, which explains how to work with SAS dates and datetimes and their formats.

View solution in original post

10 REPLIES 10
Reeza
Super User
Do you already have a SAS date and want to customize the display format? Have you already tried the standard formats?
Otherwise, you're looking for a picture format.

Date stuff starts on page 14ish
http://www2.sas.com/proceedings/sugi31/243-31.pdf
AntoineMaurin
Calcite | Level 5

We have a variable "x" which is in the format 10/12/2018 : mm/dd/yyyy and we would like to change it in the format : 12OCT2018.

Thanks for your reply

novinosrin
Tourmaline | Level 20

if it is a numeric sas date with format mm/dd/yyyy

 

All you need to do is associate date9. format 

Reeza
Super User
No, they want 01/May/2018, so the slashes. A picture or custom user format will work fine. If planning to graph or use within SAS VA I believe a user defined format is preferable, as the others don't really work.
Reeza
Super User
NVM, saw the last post with DATE9 format. OP needs to clarify their requirements regarding the need for slash or not.
ballardw
Super User

@AntoineMaurin wrote:

We are trying to change the date format from mm/dd/yyyy to dd/mm/yyyy with the months written with the three first letters of each month. Do you have an idea ? 

 

Subject:  

 

The following observations are data from four mutuals funds. The data is delimited by spaces between the variables: fund ID, Fund Inception Date, Fund End Date, and volume of transactions in dollars.  10/21/2005 12/12/2010  03/29/2009 11/11/2010 

1) Create a SAS dataset that reads in the data. Call the variables ID, FID, FED, and volume. Read it from the file mutualfunds.txt. Use a format statement for the date variables. Display them as this form : "12SEP1989".

 

Thanks


Your displayed value of "12SEP1989" is an example of the SAS supplied format DATE9.

If you read the values with the proper INFORMAT (which looks like it should be mmddyy10. ) then you only need to associate the format DATE9. with the variable when displaying as needed. However these formats work on numeric date values read as mentioned and will not work on character values. Learning point: Learn to use actual date values for flexibility.

dd/mm/yyyy is a specific date format for displaying month as a number and would be ddmmyy10.

 

If you actually do want slashes in the date with the month abbreviation you can create a custom format with Proc Format which can create some (potentially) very ugly date, time or datetime values.

 

https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/... has a PDF with much information about dates.

AntoineMaurin
Calcite | Level 5

We are not able to do it on your way. Maybe it is because we have the values in a table. Do you have an idea for doing it with this table ? 

data mutualfunds;

input ID $ FID $ FED $ Volume;
cards;
001 10/21/2005 12/12/2010 850000
002 05/01/2011 07/08/2015 1200000
003 01/01/2007 01/01/2014 19512000
004 06/23/2009 11/11/2016 15805123
005 10/21/2005 12/12/2010 815000
006 05/05/2010 07/08/2015 160000
007 01/15/2004 01/01/2014 19420000
008 03/29/2009 11/11/2010 1580123
;run;

 

 

GraphGuy
Meteorite | Level 14

Here's how I would do it ...

 

data mutualfunds;
length ID $3;
informat FID FED mmddyy10.;
format FID FED date9.;
format Volume comma10.0;
input ID $ FID FED Volume;
datalines;
001 10/21/2005 12/12/2010 850000
002 05/01/2011 07/08/2015 1200000
003 01/01/2007 01/01/2014 19512000
004 06/23/2009 11/11/2016 15805123
005 10/21/2005 12/12/2010 815000
006 05/05/2010 07/08/2015 160000
007 01/15/2004 01/01/2014 19420000
008 03/29/2009 11/11/2010 1580123
;
run;

 

Which creates a dataset that looks like this:

 

table.png

Reeza
Super User

That's because you're having date as a character variable. It needs to be a numeric variable, with a date format. Then you can change the format. This is one of the reasons to not store it in a character variable. 

 

Assuming that's what you start with, you first need to convert it using INPUT() and then apply the new format. 

 

data want;
set mutualfunds;

FID_date = input(FID, mmddyy10.);
FED_date = input(FED, mmddyy10.);
format FID_Date FED_Date date9.;

run;

@AntoineMaurin wrote:

We are not able to do it on your way. Maybe it is because we have the values in a table. Do you have an idea for doing it with this table ? 

data mutualfunds;

input ID $ FID $ FED $ Volume;
cards;
001 10/21/2005 12/12/2010 850000
002 05/01/2011 07/08/2015 1200000
003 01/01/2007 01/01/2014 19512000
004 06/23/2009 11/11/2016 15805123
005 10/21/2005 12/12/2010 815000
006 05/05/2010 07/08/2015 160000
007 01/15/2004 01/01/2014 19420000
008 03/29/2009 11/11/2010 1580123
;run;


Editor's note: see also this SAS Date Functions FAQ tutorial, which explains how to work with SAS dates and datetimes and their formats.

Astounding
PROC Star

Here's an example of reading data from text, and applying the DATE9 format to the date:

 

data test;

input datevar mmddyy10.;

format datevar date9.;

cards;

10/21/2005

12/12/2010 

03/29/2009

11/11/2010

 

You haven't shown us the structure of the text that you need to read, so you will need to apply the above tools to your data.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 10 replies
  • 88239 views
  • 0 likes
  • 6 in conversation