BookmarkSubscribeRSS Feed
jerry898969
Pyrite | Level 9

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

11 REPLIES 11
Haikuo
Onyx | Level 15

data temp2 ; 

  length zDate $9 ; 

  set temp1 ; 

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

run ;

BurntDirt
Calcite | Level 5

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!

jerry898969
Pyrite | Level 9

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. 

ballardw
Super User

DATE11. instead of DATE9.

jerry898969
Pyrite | Level 9

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

Haikuo
Onyx | Level 15

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;

Reeza
Super User

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;

ballardw
Super User

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.

BurntDirt
Calcite | Level 5

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

jerry898969
Pyrite | Level 9

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

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 11 replies
  • 4013 views
  • 7 likes
  • 5 in conversation