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;
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 adata step view/proc transpose
,tabulate
orreport
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