Struggling with dates formats, want YYYY-MM-DD

43,772

Solution 1

if you have valid SAS dates, just add a FORMAT statement to your DATA STEP.

Format busdate spotdate maturity yymmdd10. ;

SAS dates are numeric variables. They represent the number of days since 1/1/1960. You use a FORMAT to display dates.

Solution 2

Adding to CarolinaJay's answer, you normally want to keep them as numeric format, since you can do math (like "# of days since date X") with them. However, if for some reason you need a character variable, you can do this:

date_As_char=put(datevar,YYMMDD10.);

Incidentally, YYMMDD10 will actually give you YYYY-MM-DD, as you asked for; if you want a different separator, see http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000589916.htm (YYMMDDxw. format) - if you put a letter after the last D, for certain letters, you get a different separator. Like, YYMMDDn10. gives you no separator, or YYMMDDs10. gives you slashes. YYMMDDd10. gives you dashes, just like omitting the letter would. This concept also applies to MMDDYY formats, and I think a few others.

Share:
43,772
beerskij
Author by

beerskij

Updated on April 14, 2020

Comments

  • beerskij
    beerskij about 4 years

    As an absolute beginner to SAS I quickly ran into problems with date formatting.

    I have a dataset containing transaction with three types of dates: BUSDATE, SPOTDATE, MATURITY. Each transaction is represented on two lines, and I want BUSDATE and SPOTDATE from line 1 but MATURITY from line 2.

    In the original set, the dates are in YYYY-MM-DD format.

    DATA masterdata;
    SET sourcedata(rename(BUSDATE=BUSDATE2 SPOTDATE=SPOTDATE2 MATURITY=MATURITY2));
    
    BUSDATE=BUSDATE2;
    SPOTDATE=SPOTDATE2;
    
    IF TRANS_TYPE='Swap' THEN;
    MATURITY=SPOTDATE;
    
    RUN;
    

    Problem is, this returns something like 17169 (which I guess is the number of days from a certain date).

    How can I make it output in YYYY-MM-DD format - or is this approach wrong; should I first convert the date variables to some SAS date format?