BookmarkSubscribeRSS Feed
fpb1
Obsidian | Level 7
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;


6 REPLIES 6
fpb1
Obsidian | Level 7
Sorry for cross-posting. A friend suggested this solution but it seems awkward.

data clinical;
input
name $ 1-12
@13 visit_date MMDDYY10.
group $ 25 ;
datalines;
John Turner 03/12/1998 D
Mary Jones 04/15/2008 P
Joe Sims 11/30/2009 J
;
run;

AMSAS
SAS Super FREQ

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.

fpb1
Obsidian | Level 7

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;

 

AMSAS
SAS Super FREQ

Nice tries, but not quite there. Go review the documentation links below, which should help you out.

 

Working with Dates in the SAS System

Input Statement

 

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. 

andreas_lds
Jade | Level 19

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.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 577 views
  • 1 like
  • 4 in conversation