I am trying to read in some data as a date. Here are four honest tries that fail in various ways.
*error - "expecting a name";
data clinical;
input
ID $ 1-2
visit_date MMDDYY10. 4-13
group $ 15
;
datalines;
01 03/12/1998 D
02 04/15/2008 P
03 11/30/2009 J
;
run;
*error - "expecting a name";
data clinical;
input
ID $ 1-2
visit_date 4-13 MMDDYY10.
group $ 15
;
datalines;
01 03/12/1998 D
02 04/15/2008 P
03 11/30/2009 J
;
run;
*this works but the date is not a date;
data clinical;
input
ID $ 1-2
visit_date $ 4-13
group $ 15
;
datalines;
01 03/12/1998 D
02 04/15/2008 P
03 11/30/2009 J
;
run;
*error - invalid data;
data clinical;
informat visit_date MMDDYY10.;
input
ID $ 1-2
visit_date 4-13
group $ 15
;
datalines;
01 03/12/1998 D
02 04/15/2008 P
03 11/30/2009 J
;
run;
That's mixing 3 different input methods, column, formatted and list (see my reply below).
Although that's valid, I would recommend using the same input method, unless there is a reason not to.
Thanks. Though the SAS documentation you referred me to itself mixes and matches styles:
libname mylib 'permanent-data-library';
data mylib.tourdates;
infile 'input-file';
input
Country $ 1-11
@13 DepartureDate date9.
Nights;
run;
Nice tries, but not quite there. Go review the documentation links below, which should help you out.
Working with Dates in the SAS System
The problems you are having are down to mixing column input and formatted input, for the first two attempts.
The third attempt is just reading in a string.
The fourth attempts to read the date as a number, but 03/12/1998 isn't a number.
Look to use just formatted input.
I know this may sound old-school, but sometimes sticking to proven methods is useful:
data clinical;
/* define all variables in the order you want them in the dataset */
length
ID $ 2
visit_date 8
group $ 1
;
/* supply informat and format for those variables needing special treatment */
informat visit_date mmddyy10.;
format visit_date date9.;
/* just list the variables in the order in which they are in the data source */
input ID visit_date group;
datalines;
01 03/12/1998 D
02 04/15/2008 P
03 11/30/2009 J
;
run;
If more variables need a format, informat or label, then switching from length (+ informat, ...) to attrib can improve readability.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.
Lock in the best rate now before the price increases on April 1.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.