DATA Step, Macro, Functions and more

Formatting dates

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 18
Accepted Solution

Formatting dates

I am trying to format dates into sas. I tried to format the input statement to accurately read in the dates but the variable 'first' will not show in the output data. I understand how to format the proc print (i think). Please help i dont understand why it wont work. 

the code is listed below:

 

data SNL;
length host $ 18;
input Host $ & Appearances @28 First mmddyyyy10. @44 Fifth date9.;
format first fifth mmddyy8.;
datalines;
Ben Affleck 5 2/19/2000 18-May-13
Alec Baldwin 16 4/21/1990 10-Dec-94
Drew Barrymore 6 11/20/1982 03-Feb-07
Candice Bergen 5 11/8/1975 19-May-90
Chevy Chase 8 2/18/1978 06-Dec-86
Danny DeVito 6 5/15/1982 09-Jan-93
John Goodman 13 12/2/1989 07-May-94
Elliott Gould 6 1/10/1976 16-Feb-80
Tom Hanks 8 12/14/1985 08-Dec-90
Buck Henry 10 1/17/1976 19-Nov-77
Steve Martin 15 10/23/1976 22-Apr-78
Bill Murray 5 3/7/1981 20-Feb-99
Justin Timberlake 5 10/11/2003 09-Mar-13
Christopher Walken 7 1/20/1990 19-May-01
;
run;
proc print data=snl;
format first ddmmyy8.;
run;


Accepted Solutions
Solution
‎02-18-2016 06:55 PM
Super Contributor
Posts: 490

Re: Formatting dates

[ Edited ]
data SNL;
length host $ 18;
input Host & $ Appearances First: mmddyy10. Fifth: date8.;
datalines;
Ben Affleck  5 2/19/2000 18-May-13
Alec Baldwin  16 4/21/1990 10-Dec-94
Drew Barrymore  6 11/20/1982 03-Feb-07
Candice Bergen  5 11/8/1975 19-May-90
Chevy Chase  8 2/18/1978 06-Dec-86
Danny DeVito  6 5/15/1982 09-Jan-93
John Goodman  13 12/2/1989 07-May-94
Elliott Gould  6 1/10/1976 16-Feb-80
Tom Hanks  8 12/14/1985 08-Dec-90
Buck Henry  10 1/17/1976 19-Nov-77
Steve Martin  15 10/23/1976 22-Apr-78
Bill Murray  5 3/7/1981 20-Feb-99
Justin Timberlake  5 10/11/2003 09-Mar-13
Christopher Walken  7 1/20/1990 19-May-01
;
run;
proc print data=snl;
format first ddmmyy8.;
run;

View solution in original post


All Replies
Solution
‎02-18-2016 06:55 PM
Super Contributor
Posts: 490

Re: Formatting dates

[ Edited ]
data SNL;
length host $ 18;
input Host & $ Appearances First: mmddyy10. Fifth: date8.;
datalines;
Ben Affleck  5 2/19/2000 18-May-13
Alec Baldwin  16 4/21/1990 10-Dec-94
Drew Barrymore  6 11/20/1982 03-Feb-07
Candice Bergen  5 11/8/1975 19-May-90
Chevy Chase  8 2/18/1978 06-Dec-86
Danny DeVito  6 5/15/1982 09-Jan-93
John Goodman  13 12/2/1989 07-May-94
Elliott Gould  6 1/10/1976 16-Feb-80
Tom Hanks  8 12/14/1985 08-Dec-90
Buck Henry  10 1/17/1976 19-Nov-77
Steve Martin  15 10/23/1976 22-Apr-78
Bill Murray  5 3/7/1981 20-Feb-99
Justin Timberlake  5 10/11/2003 09-Mar-13
Christopher Walken  7 1/20/1990 19-May-01
;
run;
proc print data=snl;
format first ddmmyy8.;
run;
Super User
Posts: 11,343

Re: Formatting dates

Possibly too late for the "why" but for later readers.

 

This input statement:

input Host $ & Appearances @28 First mmddyyyy10. @44 Fifth date9.;

Says to always start reading the variable first in column 28.

It is important to note that if the data has tabs a tab character well generally be treated as a single character so you need to count actual character spaces used.

 

A simple by eye inspection of the posted data would tell us that the first date value appears in different positions.

In the first line of the example data I count 15 columns for the start of first. Even without running the program I expect to get a number of errors about invalid data.

Posting the log with messages would help diagnose specific issues.

 

The solution that @mohamed_zaki provided uses modified list input instead of fixed column to address that issue.

☑ This topic is solved.

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

Discussion stats
  • 2 replies
  • 216 views
  • 1 like
  • 3 in conversation