For each data file, write an appropriate DATA step to read it in. SAS INFILE STATEMENTS

Viewed 26

I am trying to write an INFILE statement in SAS that can read the data below. I was able to read this exact data without the commas in the last column. How do I incorporate that there is a comma into my code? Here is what I have tried.

data m2_d1;
INFILE '/home/u62211166/STAT674/M2/m2_d1.txt';
INPUT Car $ 1-9 Location $ 11-22 Type $ 24-28 Number 30-32;
RUN;

Sample data:

Dodge       Philadelphia   truck  1,491 
Dodge       Philadelphia   car    1,840 
Dodge       Philadelphia   SUV    8,759 
Ford        Wilmington     truck  1,270 
Ford        Wilmington     car    3,934 
Ford        Wilmington     SUV    873 
Ford        Camden         truck  4,016 
Ford        Camden         car    5,714 
Ford        Camden         SUV    1,996 
Chevrolet   Philadelphia   truck  1,140 
Chevrolet   Philadelphia   car    4,276 
Chevrolet   Philadelphia   SUV    1,984 
Toyota      Wilmington     truck  1,807 
Toyota      Wilmington     car    699 
Toyota      Wilmington     SUV    1,722 
3 Answers

For your case is not necessary to point an exact position to read, but enough to specify an informat.

data m2_d1;
   INFILE '/home/u62211166/STAT674/M2/m2_d1.txt';
   INPUT Car :$10. Location :$20. Type $ Number :comma.;
   format number comma.;
RUN;

proc print data=m2_d1 noobs;
run;

enter image description here

Informat is the correct method, but there's more than one way to do this as well.

data m2_d1;
INFILE cards;
informat car $8. Location $20. Type $8. Number comma.;
format number comma.;
INPUT Car Location  Type  Number ;
cards;
Dodge       Philadelphia   truck  1,491 
Dodge       Philadelphia   car    1,840 
Dodge       Philadelphia   SUV    8,759 
Ford        Wilmington     truck  1,270 
Ford        Wilmington     car    3,934 
Ford        Wilmington     SUV    873 
Ford        Camden         truck  4,016 
Ford        Camden         car    5,714 
Ford        Camden         SUV    1,996 
Chevrolet   Philadelphia   truck  1,140 
Chevrolet   Philadelphia   car    4,276 
Chevrolet   Philadelphia   SUV    1,984 
Toyota      Wilmington     truck  1,807 
Toyota      Wilmington     car    699 
Toyota      Wilmington     SUV    1,722 
;;;;
run;

The other answers seem to miss the main point.

You cannot use an informat with the column mode in your INPUT statement. But to read the values with commas in them as a number you have to use an informat. In this case the COMMA informat.

You could switch to reading the variable using formatted mode.

INPUT Car $ 1-9 Location $ 11-22 Type $ 24-28 @30 Number comma5. ;

Or you could switch from column mode to list mode. Either by adding the informat in-line in the INPUT statement with the : modifier.

INPUT Car $ 1-9 Location $ 11-22 Type $ 24-28 @30 Number :comma.;

Or by using an INFORMAT statement to attach the informat to the variable.

INPUT Car $ 1-9 Location $ 11-22 Type $ 24-28 @30 Number ;
informat Number comma32.;

Note that you might also need to worry about reading past the end of the line. Best to include the TRUNCOVER option on the INFILE statement.

data m2_d1;
  INFILE '/home/u62211166/STAT674/M2/m2_d1.txt' truncover;
  INPUT Car $ 1-9 Location $ 11-22 Type $ 24-28 @30 Number :comma.;
run;

PS: Your column counting seems off:

enter image description here

Related