Help using Base SAS procedures

creating new date field off of another date field

Reply
Super Contributor
Posts: 398

creating new date field off of another date field

Hello,

I have a date variable called sDate in my data set that is defined as Type=Number Length=8 Format=DATETIME20.

example value is 05AUG2012:00:00:00

I want to use sDate as the basis for creating another variable that displays the sDate as 05-AUG-12

data temp2 ;

    length zDate $9 ;

    set temp1 ;

    zDate = put(sDate, date9.) ;       

run ;

zDate within my data set is all asterisks.

Thank you for any help

Respected Advisor
Posts: 3,124

Re: creating new date field off of another date field

data temp2 ; 

  length zDate $9 ; 

  set temp1 ; 

  zDate = put(datepart(sDate), date9.) ; 

run ;

Occasional Contributor
Posts: 15

Re: creating new date field off of another date field

sDate is a numeric SAS date time variable, which is the number of seconds since 01 Jan 1960.

You can either convert that to a numeric SAS date variable, which is the number of days since 01 Jan 1960,

or you can convert it to a character date variable.

Above, you are creating a character date variable, but you are missing the proper date function to extract a date from the date time variable.

zDate = put(datepart(sDate),date9.);

This will create a character date that you can store or display.

If you want to create a numeric SAS Date variable, you can just do

zDate = datepart(sDate);

Now it's a numeric SAS Date variable that you can add or subtract.

If you want to display it, you can format it using any of the SAS date formats.

Format zDate date9.;

Good luck!

Super Contributor
Posts: 398

Re: creating new date field off of another date field

Thank you so much Hai.Kuo and burntDirt.

That worked but the format is 05AUG2012.  How can I change that to 05-AUG-12?

Thank you so much. 

Super User
Posts: 10,550

Re: creating new date field off of another date field

DATE11. instead of DATE9.

Super Contributor
Posts: 398

Re: creating new date field off of another date field

bllardw,

Thank you for your reply.  That almost worked.  It now returns 05-AUG-2012.  I need the year to only be 2 characters.

Thank you

Respected Advisor
Posts: 3,124

Re: creating new date field off of another date field

Then you have to make your format.

proc format;

     picture dttest

           low-high='%d-%b-%y'

           (datatype=date);

run;

data have;

     a='01jan2015 00:00:00'dt;

     b=put(datepart(a), dttest9.);

run;

Super User
Posts: 17,960

Re: creating new date field off of another date field

Slight modification to accommodate years such as 2001 to display as 01 and dates 5 to display as 05

proc format;

picture yymonddd

low-high = '%0d-%b-%0y'

(datatype=date);

run;

data have;

  format date yymonddd.;

  do date='01Jan2014'd to '31Jan2014'd;

  output;

  end;

run;

Respected Advisor
Posts: 3,124

Re: creating new date field off of another date field

Smiley Happy

Super User
Posts: 10,550

Re: creating new date field off of another date field

After having to go through a lot of Y2K issues I prefer to always use 4-digit years and any of my projects has to justify the use of 2-digits. At least with SAS that information is available and it is just how you are choosing to display it.

Occasional Contributor
Posts: 15

Re: creating new date field off of another date field

Hi Jerry,

I can't think of a date format that will give you that exactly.

But, if you're making a character SAS date, you can always edit it.

Try using

    substr(zDate,8,2) = '';

    zDate = compress(zDate);

There are probably a billion more ways to do this, but this was the first that popped in my head.

Gary

Super Contributor
Posts: 398

Re: creating new date field off of another date field

Thank you everyone for all your help.  I spoke with my lead and we are going to keep it with 4 digits for the year.   He always says no whenever I ask to change things but this time he agreed it is a better way to go.

Thank you

Ask a Question
Discussion stats
  • 11 replies
  • 375 views
  • 7 likes
  • 5 in conversation