Parsing header line separately in SAS

2019-05-15 16:32发布

问题:

I have input file, in which the first line has header information. (Data values which are tab-separated). Amongst these values, there is an integer value that specifies how the rest of the file should be parsed. If this value is less than a threshold value, then the file should be parsed in one way, otherwise if the value is greater that the threshold value, then the data should be parsed in a different way.

%let isNew = Undefined;

data header;
infile "&infile" OBS=1;
INPUT Agency $ Status $ Num $ fdate sdate;
if fdate < 20130428 then
 %let isNew = false;
else
 %let isNew = true;
run;

data factors;
infile "&infile" missover FIRSTOBS=2 LRECL=1000;
if isNew = false then
 input
   @1   poolno                  $6.
   @7   factor                  9.8 @;
else
 input
   @3   poolno                  $6.
   @9   factor                  9.8 @;

(some more input parsing code)

In the above code, I have defined a variable isNew and setting that variable to true/false depending on the condition check. In the subsequent data block, I am using the value of this variable to decide which way the file is to be parsed.

A sample input file is (the value to be examined is in bold):

FHLMC UPDATE #1 20130130 20130306

138788024201321000

140379000000000000

I am new to SAS. Any suggestions please?

回答1:

You are mixing up data step variables and macro variables. You can't conditionally execute a %LET statement like that (and you don't want to). CALL SYMPUT is how you'd create a macro variable, if you wanted to; but in this case you don't, as you can perform this all in one data step:

data want;
informat fdate sdate YYMMDD8.;
retain Agency Status Num fdate sdate;
if _n_ = 1 then do;
  input Agency $ Status $ Num $ fdate sdate;
end;
else do;
    if fdate < '28APR2013'd then input
       @1   poolno                  $6.
       @7   factor                  9.8;
    else
     input
       @3   poolno                  $6.
       @9   factor                  9.8;

    output;
end;
datalines;
MyAgency MyStatus MyNum 20130401 20130501
MyPool123456783123
MyPoo2435678904123
;;;;
run;

Now, one thing to note - You normally don't add the decimal to the informat (ie, @9 factor 9.8) unless you have a number that has no decimal and you want to add it. IE, in the data I created, I did not have a decimal (123456783) and it will add one (1.23456783). However, if the decimal is already in the data, ie 1.23456783 is in your data file, then you should just input it as 9. and let SAS place the decimal for you. Also, if you test this make sure to move the datalines over to the first column (indenting them 4 will cause this to fail).

Also, you need to learn how to work with SAS dates - see how I changed it slightly.



标签: sas sas-macro