Help using Base SAS procedures

Dates = numbers? HOW

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 17
Accepted Solution

Dates = numbers? HOW

Hi

I had dates in the format: DD/MM/YYYY

I wanted these to be represented as numbers. So I did:

data have; set have;

date2= date*1;

run;

This command produced large negative numbers. No decipherable ddmmyyy was found (im looking for numeric dates without the commars).

I then, performed the following command:

data have;   set have;   format date ddmmyyn8.; run

This produced a pleasing result in the form of ddmmyyyy.

But however SAS still didnt recognise my date as numeric. When I multipled by 1, I still received large negative numbers. How do I solve this problem?

I need ddmmyyyy to be in number form in order fo this command to work:

data have; set have;

IF DATE>0 then MMYY = substr(DATE, 4, 7);

RUN;


Accepted Solutions
Solution
‎07-09-2012 10:22 AM
Respected Advisor
Posts: 3,124

Re: Dates = numbers? HOW

How about combine code from Ksharp and Alpay?

proc import datafile='c:\temp\test.dbf' out=have dbms=dbf replace;run;

data want;

set have;

  newdate = INPUT(PUT(date,ddmmyyn8.),best12.);

run;

Haikuo

View solution in original post


All Replies
Super Contributor
Posts: 349

Re: Dates = numbers? HOW

Hi,

If you have posted some sample data it would have helped...

data have;

input date $;

cards;

30102011

;

run;

data want;

set have;

newdate=input(date,ddmmyy8.);

format newdate date9.;

run;

Thanks,

Shiva

Occasional Contributor
Posts: 17

Re: Dates = numbers? HOW

hmm i follwed your codes but didnt seem to work. I received an error message.

If you could help me identify what im doing wrong that would be rlly helpful. Here is my data:

Attachment
Super User
Posts: 9,682

Re: Dates = numbers? HOW

Your file is dbf file and the date variable has already been numberic after importing SAS and its formate is not DDMMYY but MMDDYY.

proc import datafile='c:\test.dbf' out=have dbms=dbf replace;run;
data want;
 set have;
 mmyy=put(date,mmyys.);
run;

Ksharp

Occasional Contributor
Posts: 17

Re: Dates = numbers? HOW

Hey thanks for your input.

BUT In my dataset would like dates to be numbers and without the '/'

To illustrate: if the date was 31/12/2011 I would like it to be 31122011.

And if I multiply 31122011 by 1 it should still be 31122011.   Not a large negative number (I commonly find I can't plus or minus numbers to the date formats)

Super Contributor
Posts: 349

Re: Dates = numbers? HOW

Hi,

I dont understand the logic why you want the date in this format 31122011 .Any how you can just apply the format like this..SAS internally stores date values in numbers itself.

data have;

set dbf;

date1=date*1;

format date1 mmddyy7.;

RUN;

Thanks,

Shiva

Occasional Contributor
Posts: 17

Re: Dates = numbers? HOW

Hi

I would like the date in the numeric format because then I can plus ten years to the year or minus ten years to the year. I'm looking to edit my dates later on my in my analysis.

Via your method date 1 still is not recognised as a number. When you multiply by 1 or minus 1 I am still receiving a large negative. Furthermore, is it possible to have it in the format ddmmyyyy OR yyyymmdd?

Frequent Contributor
Posts: 95

Re: Dates = numbers? HOW

Date values are stored as the number of days since 1/1/1960 in SAS and displayed with date formats like mmddyys10. (MM/DD/YYYY)/date9.(DDMONYYYY), etc.

SAS has specific functions to do date arithmetic like increment/decrement dates by week/month/quarter/year or count the date intervals between two dates.

For some examples/details, please see Art Carpenter's paper on using SAS dates and times http://www.lexjansen.com/pharmasug/2005/tutorials/tu01.pdf

The following code snippet will turn a date value 31/12/2012 into an integer value of 31122012.

data _null_;

  date = '31Dec2012'd;

  newdate = INPUT(PUT(x,ddmmyyn8.),best12.);

  put newdate=;

run;

Occasional Contributor
Posts: 17

Re: Dates = numbers? HOW

using this method I only have 1 observation. I have attached my data above. What can I change '31dec2012' to read all my data not just 1?Capture.JPG

Solution
‎07-09-2012 10:22 AM
Respected Advisor
Posts: 3,124

Re: Dates = numbers? HOW

How about combine code from Ksharp and Alpay?

proc import datafile='c:\temp\test.dbf' out=have dbms=dbf replace;run;

data want;

set have;

  newdate = INPUT(PUT(date,ddmmyyn8.),best12.);

run;

Haikuo

Super User
Posts: 5,085

Re: Dates = numbers? HOW

Once you understand how SAS stores date values, this would be the way to add 10 years:

data want;

   set have;

   newdate = intnx('year', date, +10, 'same');

   format date newdate ddmmyyn8.;  /* or any other date format you would like to see */

run;

But first, it is important to understand SAS's date scale.

Good luck.

☑ This topic is SOLVED.

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

Discussion stats
  • 10 replies
  • 634 views
  • 8 likes
  • 6 in conversation