3

I am new to SAS and having a hard time figuring out when should the simple If-Then-else and when should %IF-%THEN-%ELSE should be used. As an example code below:

%let inFile = %scan(&sysparm, 1, " ");
%macro read_data(infile);
data want;
infile "&infile" LRECL=1000;
retain fdate;
if _n_ = 1 then do;
  input Agency $ Status $ Num $ fdate sdate;
end;
else do;
   %if fdate < 20130428  %then
   input
   @1   poolno                  $6.
   @7   factor                  9.8 @;
   %else
   input
   @1   rectype                 $1
   @3   poolno                  $6.
   @9   factor                  9.8 @;

   @18 pfactor                 9.8;
output;
end;
drop Agency Status Num sdate;
run;
proc print data=want;
run;
%mend read_data;
%read_data(&inFile);

I am trying to get the first line(header) and taking the parameter fdate. Based on the value of this parameter, I am parsing the subsequent input lines differently. But this does not seem to work, as only the second input part runs (always getting parameter 'rectype' in output).

Any suggestions as what i might be doing wrong?

user673218
  • 411
  • 2
  • 8
  • 20

4 Answers4

2

I see you have C++ as one of your tags, and that you are just getting started with SAS. So I'll try to provide an answer specific to your background.

The easiest way to understand the distinction between SAS Macro commands and the commands in the DATA step or in several procs with the same name, like %if vs. if, is to think of SAS Macro commands as equivalent to C/C++ pre-processor (CPP) directives. CPP and SAS MAcro are both macro languages, and although they are not exactly the same kind of language, they share two important and initially confusing characteristics: they are text processors; and they are executed as a separate step before the main code is processed.

There are places where this approximation breaks down, but as a beginner to SAS with background in C/C++, it is a good place to start.

Leo
  • 2,775
  • 27
  • 29
1

The macro statement %if is compiled before any data step statement. That means you are generally not able to use a data step variable in your logic expression. When the macro processor is compiling the macro statement, data step variable doesn't exist yet.

kylechun9
  • 196
  • 5
0

In the example above, the %IF condition is based on a datastep variable/value. This should indicate it can be achieved using a datastep 'if' rather than a %IF.

You have already received an answer to this in your previous question, https://stackoverflow.com/a/15341502/108797

Community
  • 1
  • 1
Chris J
  • 7,549
  • 2
  • 25
  • 25
0

If you do %if fdate < 20130428 SAS compares literals fdate and 20130428 not the value od fdate and 20130428.

If you had a macro variable named fdate you would do %if &fdate < 20130428.

In your case fdate is a variable in a dataset, so use if not %if, but it seems like you are trying to create a data step with a macro so just using if will probably not work in this case (depends on what are you trying to get).

Dejan Peretin
  • 10,891
  • 1
  • 45
  • 54