0
votes

A user's question here: Summing a Column By Group In a Dataset With Macros

inspired me to try a "dirty transpose" with macro calls. Below is my attempt. I use the symput statement to populate a macro and use that macro to populate a variable in the data step. However (and interestingly), the output shows that only the value of &test is used to for the variable in the data step. Once the datastep completes, &test takes the last value from the datastep. For instance, in this example, the first run creates a variable "value" in the out dataset. Print the value of &test, you get "May".

data have;
  infile datalines;
  input Month  $ Cost_Center     Account  $   Actual    Annual_Budget;
  datalines;
  May      53410           Postage       23      134
  May      53420           Postage       7       238
  May      53430           Postage       98      743
  May      53440           Postage       0       417
  May      53710           Postage       102     562
  May      53410           Phone         63      137
  May      53420           Phone         103     909
  May      53430           Phone         90      763
  June     53410           Postage       13      134
  June     53420           Postage       0       238
  June     53430           Postage       48      743
  June     53440           Postage       0       417
  June     53710           Postage       92      562
  June     53410           Phone         73      137
  June     53420           Phone         103     909
  June     53430           Phone         90      763
  ;
run;

proc sort data=have; by account  month  ; run;
%let mymacro = sum;
%let test = value;

data out(drop=cost_center month actual annual_budget sum );
  set have;
  by account month ;
  retain sum;

  if first.month then sum = 0;
  sum = sum + actual;
  if last.month then do;
    call symput("test", month);
    &test = sum;
  end;

  if last.account then output;
run;

%put &test;
1
A running data step can not modify the PDV constructed at compilation time. That is what your unexperienced intuition is expecting from symput ("test",...); &test … . All & macro references in code are resolved at pre-compilation and become part of the source code stream that is the data step source code. A second inspiration might be to code a data step view/proc transpose, tabulate or report report. My experience with pivoting of 'data' into 'meta-data' is that it is typically an operation of reporting and not one of data processing for downstream analytics.Richard
If you really need to dynamically store in preexisting variable by name than use PUTVARC or PUTVARN. If you need the same for not existing try PROC LUA.Lee

1 Answers

0
votes

This is a classic mistake with CALL SYMPUT. It is not possible to use a macro variable assigned through CALL SYMPUT in the same datastep that creates or assigns the value. The value of test will always be value during datastep execution.

The CALL SYMPUT documentation provides detailed information on this: http://support.sas.com/documentation/cdl/en/mcrolref/62978/HTML/default/viewer.htm#p09y28i2d1kn8qn1p1icxchz37p3.htm

You are trying to create variables May and June on-the-fly. This cannot happen because the datastep structure is defined during compile time and these variables are not known at that time; rather created during datastep execution.