BoTz
BoTz

Reputation: 492

Read DATA INPUT column statement conditionally

I'm relatively new to SAS (using SAS EG if it matters).

I have several previously written programs that read new data files. For each type of data file there's a separate program with it's specific INPUT column statement.

For example, one program would have:

DATA data1;
   INFILE 'D:\file.txt' noprint missover;
INPUT
ID      1 - 8
NAME  $ 9 - 20;
run;

whereas another program would have other definitions. for example:

INPUT
ID      1 - 5
NAME  $ 6 - 20

Each data file contains hundreds of variables, so the INPUT column statement in each program is very long. However, the rest of these programs are completely identical.

My intention is to combine these programs into one,

I have two questions:

  1. Is it possible to combine these programs with a conditional INPUT column statement?

  2. Is it possible to read the definition of each file type columns from a variable? (Thus enabling me to define it elsewhere in the workflow or even to read it from an external file)

Upvotes: 0

Views: 396

Answers (1)

deristnochda
deristnochda

Reputation: 585

It seems like you use text files with a fixed width definition. For these you can each specify a format file of the form

column, type, start, end

and then read that file first in order to build the INPUT statement. column is the column name, type one of n (numeric) or c (character), start and end start and end positions for this column.

You would wrap this into a MACRO like this:

%macro readFile(file, output);
  %local input_statement;
  /* First, read the format file that contains the column details. */
  data _null_;
    infile "&file..fmt" dlm="," end=eof;
    input column $ type $ start end;
    length input_statement $ 32767;
    retain input_statement "input";
    if type = "c" then type = "$";
    else type = "";
    input_statement = catx(" ", input_statement, column, type, start, "-", end);
    if eof then call symputx("input_statement", input_statement);
  run;

  /* Read the actual file. */
  data &output.;
    infile "&file.";
    &input_statement.;
  run;

%mend;

For a file file.txt the macro needs the format file to be named file.txt.fmt in the same path. Call the macro as

%readFile(%str(D:\file.txt), data1);

Upvotes: 2

Related Questions