The SAS Output Delivery System and reporting techniques

Date format

Reply
Contributor
Posts: 43

Date format

Hi there,
I have to use a date format like this: 01-jan-07 but I can not find out how to use it in SAS. I know there is a similar format: 01.jan.07 or 01jan07 but how can I put the dash?

Thanks in advance!
Juan Vte.
SAS Super FREQ
Posts: 8,864

Re: Date format

Hi:
If you make a PICTURE format and use a DATE "directive", you can insert the hyphens into any kind of date format you want to design. Some of the SAS formats allow you to specify a separator, but the DATE7 and DATE9 formats don't fall into that category.

In this program, I have a PROC FORMAT that sets up the special date PICTURE format and then my PROC PRINT uses the format -- and shows some other formats by way of comparison.

If you need more help using PROC FORMAT for setting a PICTURE format, I recommend that you start with this SUGI paper:
http://www2.sas.com/proceedings/sugi31/243-31.pdf or contact Tech Support.

cynthia
[pre]
*** the code;
** first make some test data;
data testdate;
length name $20;
infile datalines dsd;
input name $ bdate : date9.;
bdate1 = bdate;
bdate2 = bdate;
bdate3 = bdate;
bdate4 = bdate;
bdate5 = bdate;
bdate6 = bdate;
return;
datalines;
"Sam Waterston",15NOV1940
"Petula Clark",15NOV1932
"Liv Tyler",01JUL1977
"Emilio Estevez",12MAY1962
;
run;

** using date directives to make a picture format;
proc format;
picture bd low-high='%0d-%b-%y' (datatype=date);
picture bd_alt low-high = '%0d-%b-%Y%y' (datatype=date);
run;

** formatting date multiple ways;
proc print data=testdate label;
title 'Show Biz Bdays';
label bdate = 'Internal Number for Date'
bdate1 = 'Used DATE7. format'
bdate2 = 'Picture Format Dashes, 2-digit year'
bdate3 = 'Picture Format Dashes, 4-digit year'
bdate4 = 'Used DATE9. format'
bdate5 = 'Used MMDDYY10. format'
bdate6 = 'Used ddmmyyd11. format';
format bdate1 date7. bdate2 bd. bdate3 bd_alt.
bdate4 date9. bdate5 mmddyy10. bdate6 ddmmyyd10.;
run;
[/pre]
N/A
Posts: 0

Re: Date format

data testdates;
input id_no name $ start_date date9.;
cards;
1 Venu 15NOV2005
2 Prem 23DEC2005
3 Tyler 25JAN2006
4 Clark 28JAN2006
5 Sam 02MAR2006
;
run;
/*proc print data = testdates;
format start_date ddmmyyd10.;
run;*/
proc report data = testdates nowd split='#' headline headskip;
column id_no name start_date;
define id_no/display "IDENTIFICATION#NUMBER";
define name/display "NAME OF THE #EMPLOYEE";
define start_date/display "DATE OF #JOINING" format=ddmmyyd10.;
run;
N/A
Posts: 0

Re: Date format

Hi,

You can seperate the Day ,Month and Year from the DATE varaible by using the functions like PUT,SUBSTR,YEAR.
Convert the DATE varaible from numeric to Character Data Type by using PUT function ,Beacuse DATE varaible is a Numeric Data Type.After seperating the Day,Month and Year from the DATE varaible.

You can concatenate the Day,Month,Year and hypen symbol ('-') by using the CAT function or Concatente symbol (||).

Program:--

data testdates;
input id_no name $ st_date date9.;
date = st_date;
new1 = put(date,date.);
month = substr(new1,3,3);
day = substr(new1,1,2);
year = year(st_date);
new_date = compress(day||'-'||month||'-'||year);
cards;
1 Venu 15NOV2005
2 Prem 23DEC2005
3 Tyler 25JAN2006
4 Clark 28JAN2006
5 Sam 02MAR2006
;
run;

proc print data = testdates;
var day month year new_date;
run;
N/A
Posts: 0

Re: Date format

Posted in reply to deleted_user
Converting dates to strings and substringing is a poor idea IMHO. There are many very good functions in SAS that allow parsing out the date elements.

The Day() and Year() functions will extract the values, and the month name can be added by using the MonName. format.

Having said that, the most elegant solution remains the one that creates a custom picture format. As usual, the Queen of ODS demonstrates that there is little she can't do to give you data as you like it.

Kind regards

David


P.S. again for forum support: and the dictionary doesn't know ODS either. Could the dictionary be loaded with the procedure names and standard SAS syntax?
Contributor
Posts: 49

Re: Date format

Posted in reply to deleted_user
David,

I agree with you in terms that DAY, MONTH, YEAR and SUBSTR etc. is generally not the most efficient way creating these types of display formats and I would generally use Cynthia's PICTURE format (very under-used in my experience!).

However, if you work in clinical trials as I do, you get plenty of partial dates (day or day and month unknown) so to list this data you often have to create character equivalents of the form: "XX-JUN-2007" or similar.

In this case, I would use the character functions.

Cheers,
Lawrence
Ask a Question
Discussion stats
  • 5 replies
  • 279 views
  • 0 likes
  • 4 in conversation