Monday, May 28, 2012

Using Multiple SET Statements

When you use multiple SET statements,
  • processing stops when SAS encounters the end-of-file (EOF) marker on either data set (even if there is more data in the other data set) 
  •  
  • the variables in the program data vector (PDV) are not reinitialized when a second SET statement is executed. 
This is useful when want to combine the summary info with the details rows of the data set.

suppose sasuser.summary has one var named sum, with one obs valued as 100; sasuser.monthsum has two vars: month sale:
month sale
1      28
2      32
3      40
we want to combine them together to calculate the percentage for each month.

data a; 
  if _N_=1 then set sasuser.summary;
  set sasuser.monthsum;
  pct=sale/sum;
run;

the automatic variable _N_ keeps track of how many times the DATA step has begun to execute. The following DATA step uses _N_ to keep SAS from reaching the EOF marker for Sasuser.Summary after the first iteration of the step. Since the variables in the PDV will not be reinitialized on each iteration, the first value of Summary.Cargosum will be retained in the PDV for each observation that is read from Sasuser.Monthsum

1 comment:

  1. ****** Using an Index to Combine Data;

    data work.profit work.errors;
    set sasuser.dnunder;
    set sasuser.sale2000(keep=routeid
    flightid date rev1st revbusiness
    revecon revcargo)key=flightdate;
    if _iorc_=0 then do;
    Profit=sum(rev1st, revbusiness, revecon,
    revcargo, -expenses);
    output work.profit;
    end;
    else do;
    _error_=0;
    output work.errors;
    end;
    run;

    ReplyDelete