Pregunta

I need to understand how SAS reads/executes data steps. When I've looked up info on how SAS reads data steps, all I seem to find is info on how it reads for merging purposes, which i don't understand in relation to a regular data step. Lets say, for example, I have this line of code:

data work.DATA;
  if amount_a= . then
     amount_a= 1;
  amount_b= 1;
  amount_a= . ;
  total = (amount_a + amount_b) + 0 ;
run;

Now, given this, what would "total" equal? I want to know, essentially, how SAS would read this step -- which line would it read/execute first? Does it start at the last, then work its way up? Or start at the top, and work its way down? Thanks.

¿Fue útil?

Solución

A SAS data step processes code from top to bottom, beginning with the DATA statement and ending with the RUN; statement. Data steps have an implied OUTPUT; statement included immediately before the RUN; if the code does not have an explicit output statement.

Since SAS is an "interpreted" language, the code for each data step is compiled before execution. Part of the compilation involves creating a structure called the Program Data Vector (PDV) which contains execution attributes of all variables used by the program. Variables are defined to the PDV in the order they appear in the code (from top to bottom).

A handy debugging tool is the PUTLOG statement, with which you can cause output to be written to your SAS log file during program execution. For example, consider this:

data work.DATA;
   if amount_a= . then
      amount_a= 1;
   amount_b= 1;
putlog amount_a= amount_b=;
   amount_a= . ;
putlog amount_a= amount_b=;
   total = (amount_a + amount_b) + 0 ;
putlog amount_a= amount_b= total=;
   output;
run;

Notice that I added an explicit OUTPUT; statement to illustrate. The result is a SAS data set with one observation and three variables. Your variable total will be a missing value because at the time it is calculated, amount_a is missing. You will also get a NOTE in the SAS log indicating that "Missing values were generated".

The best place to learn all about how SAS does this is in the SAS Language Reference: Concepts book. Here is a link to the book for SAS version 9.3. In particular, read the chapter on Data Step Processing.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top