0
votes

My company just switched from R to SAS and I am converting a lot of my R code to SAS. I am having a huge issue dynamically declaring variables (macro variables) in SAS.

For example one of my processes needs to take in the mean of a column and then apply it throughout the code in many steps.

%let numm =0;

I have tried the following with my numm variable but both methods do not work and I cannot seem to find anything online.

PROC MEANS DATA = ASSGN3.COMPLETE mean;
#does not work
&numm = VAR MNGPAY;
run;

Proc SQL;
#does not work
&numm =(Select avg(Payment) from CORP.INV);
quit;
2
Macro variables usually should not store data values. They're just text. Among other things, they have no concept of numeric precision, so you lose precision when you convert to a macro variable. SAS is more like SQL than R in a lot of ways; store your means in a dataset.Joe
Yeah, I definitely have been noticing that SAS is more of a data management system that can perform statistics as well. R seems much better for high level analysis.Chris
SAS is quite capable of high level analysis as well as R, and for some things is quite a lot faster. It's just different, and requires thinking in a different way to program. What you're doing above is like using global variables in C (or R, for that matter) inside a function; you wouldn't do that in SAS. However, the same results can be achieved, often in a much simpler fashion, using more idiomatic processes.Joe

2 Answers

0
votes

I would highly recommend buying a book on SAS or taking a class from SAS Training. SAS Programming II is a good place to start (Programming I if you have not programmed anything else, but that doesn't sound like the case). The code you have shows you need it. It is a complete paradigm shift from R.

That said, try this:

proc sql noprint;
select mean(payment) into :numm from corp.inv;
quit;

%put The mean is: &numm;
0
votes

Here's the proc summary / data step equivalent:

proc summary data = corp.inv;
  var payment;
  output out = inv_summary mean=;
run;

data _null_;
  set inv_summary;
  call symput('numm',payment);
run;

%put The mean is: &numm;

Proc sql is a more compact approach if you just want a simple arithmetic mean, but if you require more complex statistics it makes sense to use proc summary.