Here's my macro, below. The variables var1, var2, var3, VR, and maxwt are not input to the macro, and are not on a file. They are intermediate results which are calculated inside the macro. When I execute in in SAS 9.3, I get a message like this:
1 var1 = &dcount1 * (&spop1/&refpr1)**2;
----
180
ERROR 180-322: Statement is not valid or it is used out of proper order.
Macro:
%macro confi (aart,dcount1,dcount2,dcount3,spop1,spop2,spop3,refpr1=0.53468238,refpr2=0.30153350,refpr3=0.16378412);
var1 = &dcount1 * (&spop1/&refpr1)**2;
var2 = &dcount1 * (&spop2/&refpr2)**2;
var3 = &dcount3 * (&spop1/&refpr3)**2;
VR = var1+var2+var3;
maxwt = max(&refpr1 &refpr2 &refpr3);
CI_low = (VR/ 2*&aart)) * cinv(0.025,2*(&aart**2)/VR);
CI_high = (( VR + maxwt**2) / (2*(&aart+maxwt)))* cinv(0.975, 2*(&aart+maxwt)**2/(VR + maxwt**2));
%put &aart CI_low CI_high;
%mend confi;
%confi (aart=1000, dcount1=20, dcount2=70, dcount3= 10. spop1=3000, spop2=3000, spop3=200);
That macro would have to be executed inside of a data step. Those are data step variables and statements and can't be executed in open code.
You also can't use %put for that purpose - you need to use put.
You are missing the datastep to calculate all your non-macro variables.
When you call a MACRO to execute, it is a BEST PRACTICE not to put ; at the end of the line.
On the CI_LOW arithmetic operation you have mismatch parenthesis.
When you call the macro %confi, you are assigning dcount3 variable value wrong, you need a comma, not a dot.
When you declare a Macro, you don't assign values there, only declare variables.
On the Max Function, the variables need to be separated by commas.
Try this:
Options Macrogen Symbolgen;
%macro confi (aart,dcount1,dcount2,dcount3,spop1,spop2,spop3,refpr1,refpr2,refpr3);
data _null_;
var1 = &dcount1 * (&spop1/&refpr1)**2;
var2 = &dcount1 * (&spop2/&refpr2)**2;
var3 = &dcount3 * (&spop1/&refpr3)**2;
VR = var1+var2+var3;
maxwt = max(&refpr1, &refpr2, &refpr3);
CI_low = (VR/ 2*&aart) * cinv(0.025,2*(&aart**2)/VR);
CI_high = (( VR + maxwt**2) / (2*(&aart+maxwt)))* cinv(0.975, 2*(&aart+maxwt)**2/(VR + maxwt**2));
put 'low =' CI_low;
put 'high = ' CI_high;
run;
%put &aart;
%mend confi;
%confi (aart=1000, dcount1=20, dcount2=70, dcount3= 10, spop1=3000, spop2=3000, spop3=200, refpr1=0.53468238, refpr2=0.30153350, refpr3=0.16378412)
Related
data example1;
input var1 var2 var3;
datalines;
10 11 14
3 5 8
0 1 2
;
data example2;
input var;
datalines;
1
2
8
;
Let's say that the number of var variables depending on data input. I want to put that number to macro variable and use in another data step, for example:
%macro m(input);
data &input.;
set &input.;
array var_array[*] var:;
%let array_dim = dim(var_array);
do i = 1 to &array_dim;
var_array[i] = var_array[i] + 1;
end;
drop i;
run;
data example2;
set example2;
var2 = var * &array_dim; /* doesn't work */
run;
%mend;
%m(example1);
%let array_dim = dim(var_array); doesn't work in second data step, because dim(var_array) isn't evaluated, but %eval or %sysevalf in declaring the macro variable does't work here. How to do that correctly?
You are mixing up macro code and data step code in a way that is not supported in SAS. If you want to assign a macro variable a value that you're generating as part of a data step, you need to use call symput.
Also, if you create a macro variable during a data step, you cannot resolve it during the same data step in the way that you are attempting to do (unless you use the resolve function...). It's easier just to use a data set variable for this.
So here's a fixed version of your code that I think probably does what you want:
%macro m(input);
data &input.;
set &input.;
array var_array[*] var:;
array_dim = dim(var_array);
/*Only export the macro variable once, for the first row*/
if _n_ = 1 then call symput('array_dim_mvar', array_dim);
do i = 1 to array_dim;
var_array[i] = var_array[i] + 1;
end;
drop i;
run;
data example2;
set example2;
var2 = var * &array_dim_mvar;
run;
%mend;
%m(example1);
I want to do simple math on numeric date, format YYYYMM, but when I run my monthend_add macro in another macro it changes it to character. Can anyone explain this?
/* macro to add months and keep YYYYMM format */
%Macro monthend_add(me, add);
%let months = %eval(&me / 100 * 12 + %sysfunc(mod(&me, 100)) + &add - 1);
%eval(&months / 12 * 100 + %sysfunc(mod(&months, 12)) + 1);
%mend monthend_add;
%let me = 201512;
%let me = %monthend_add(&me, 1);
%put me: &me, %datatyp(&me);
%macro now_char;
%let me = 201512;
%let me = %monthend_add(&me, 1);
%put me: &me, %datatyp(&me);
%mend;
%now_char
In both cases (global scope in first, inner scope in second) you are returning the semicolon in your monthend_add macro. To fix remove this as follows:
%Macro monthend_add(me, add);
%let months = %eval(&me / 100 * 12 + %sysfunc(mod(&me, 100)) + &add - 1);
%eval(&months / 12 * 100 + %sysfunc(mod(&months, 12)) + 1)
%mend monthend_add;
In the first instance, it seems the returned semicolon terminated your assignment statement. Hence &me=201512 and %datatype(201512)=NUMERIC.
In the second instance (within a macro) it seems this was not the case, and so &me=201512; and %datatyp(201512;)=CHAR
I would guess the reason is that in global context, the macro executes immediately before the assignment is finished (hence terminates itself at the first semicolon).
Whereas the in the second (inner macro) context, the monthend_add macro cannot be run until the now_char is called - hence when now_char is compiled, the space for the value of me is pre-allocated, into which the returned semicolon is then subsequently stored.
As a final note - remember that (technically) SAS macro variables only ever store text! 65,534 characters to be exact.
SAS is interpreting in_year_tolerance and abs_cost as text vars in the below if statement. Therefore the if statement is testing the alphabetical order of the vars rather than the numerical order. How do I get SAS to treat them as numbers? I have tried sticking the if condition, and the macro vars, in %sysevalf but that made no difference. in_year_tolerance is 10,000,000 and cost can vary, but in the test run starts around 20,000,000 before dropping to 9,000,000 at which point it should exit the loop but doesn't.
%macro set_downward_caps(year, in_year_tolerance, large, small, start, end, increment);
%do c = &start. %to &end. %by &increment.;
%let nominal_down_large_&year. = %sysevalf(&large. + (&c. / 1000));
%let nominal_down_small_&year. = %sysevalf(&small. + (&c. / 100));
%let real_down_large_&year. = %sysevalf((1 - &&nominal_down_large_&year.) * &&rpi&year.);
%let real_down_small_&year. = %sysevalf((1 - &&nominal_down_small_&year.) * &&rpi&year.);
%rates(&year.);
proc means data = output.s_&scenario. noprint nway;
var transbill&year.;
output out = temporary (drop = _type_ _freq_) sum=cost;
run;
data _null_;
set temporary;
call symputx('cost', put(cost,best32.));
run;
data temp;
length scenario $ 30;
scenario = "&scenario.";
large = &&real_down_large_&year.;
small = &&real_down_small_&year.;
cost = &cost.;
run;
data output.summary_of_caps;
set output.summary_of_caps temp;
run;
%let abs_cost = %sysevalf(%sysfunc(abs(&cost)));
%if &in_year_tolerance. > &abs_cost. %then %return;
%end;
%mend set_downward_caps;
Use %sysevalf(&in_year_tolerance > &abs_cost,boolean).
As you have seen, compares are text based. If you put it in an %eval() or %sysevalf() the values will be interpreted as numbers.
The ,boolean option lets it know you want a TRUE/FALSE.
How do I print out the data type of a macro variable in the log
%macro mymacro(dt2);
%LET c_mth = %SYSFUNC(intnx(month,&dt2.d,-1,e),date9.) ;
%put &c_mth;
%mend;
mymacro('01sep2014')
I have a bunch of macro variables assigned using a %let or into:
my problem is I'm trying to do a bunch of boolean condition on dates but I suspect that some of my variables are strings and some are dates
I have casted them in my code but to triple check there is surely a way to return something to the log
I want something similar to using str() or mode() or is.numeric() in R
H,
The SAS macro language is weird. : )
As Reeza said, macro variables do not have a type, they are all text.
But, if you use Boolean logic (%IF statement), and both operands are integers, the macro language will do a numeric comparison rather than a character comparison.
So you can use the INPUTN() function to convert the date strings to SAS dates (number of days since 01Jan1960), and then compare those. Here's an example, jumping off from your code:
%macro mymacro(dt1,dt2);
%local c_mth1 c_mth2 n_mth1 n_mth2;
%let c_mth1 = %sysfunc(intnx(month,&dt1.d,-1,e),date9.) ;
%let c_mth2 = %sysfunc(intnx(month,&dt2.d,-1,e),date9.) ;
%let n_mth1 = %sysfunc(inputn(&c_mth1,date9.)) ;
%let n_mth2 = %sysfunc(inputn(&c_mth2,date9.)) ;
%put &c_mth1 -- &n_mth1;
%put &c_mth2 -- &n_mth2;
%if &n_mth1<&n_mth2 %then %put &c_mth1 is before &c_mth2;
%else %put &c_mth1 is NOT before &c_mth2;
%mend;
Log from a sample call:
236 %mymacro('01feb1960','01mar1960')
31JAN1960 -- 30
29FEB1960 -- 59
31JAN1960 is before 29FEB1960
--Q.
Macro variables do not have a type, they are all text.
You have to make sure the variable is passed in a way that makes sense to the program and generates valid SAS code.
%let date1=01Jan2014;
%let date2=31Jan2014;
data _null_;
x = "&date1"d > "&date2"d;
y = "&date2"d > "&date1"d;
z = "&date2"d-"&date1"d;
put 'x=' x;
put 'y=' y;
put 'z=' z;
run;
Log should show:
x=0
y=1
z=30
If your macro variables resolve to date literals, you can use intck combined with %eval to compare them, e.g.
%let mvar1 = '01jan2015'd;
%let mvar2 = '01feb2015'd;
/*Prints 1 if mvar2 > mvar1*/
%put %eval(%sysfunc(intck(day,&mvar1,&mvar2)) > 0);
I want to have a mean which is based in non zero values for given variables using proc means only.
I know we do can calculate using proc sql, but I want to get it done through proc means or proc summary.
In my study I have 8 variables, so how can I calculate mean based on non zero values where in I am using all of those in the var statement as below:
proc means = xyz;
var var1 var2 var3 var4 var5 var6 var7 var8;
run;
If we take one variable at a time in the var statement and use a where condition for non zero variables , it works but can we have something which would work for all the variables of interest mentioned in the var statement?
Your suggestions would be highly appreciated.
Thank you !
One method is to change all of your zero values to missing, and then use PROC MEANS.
data zeromiss /view=zeromiss ;
set xyz ;
array n{*} var1-var8 ;
do i = 1 to dim(n) ;
if n{i} = 0 then call missing(n{i}) ;
end ;
drop i ;
run ;
proc means data=zeromiss ;
var var1-var8 ;
run ;
Create a view of your input dataset. In the view, define a weight variable for each variable you want to summarise. Set the weight to 0 if the corresponding variable is 0 and 1 otherwise. Then do a weighted summary via proc means / proc summary. E.g.
data xyz_v /view = xyz_v;
set xyz;
array weights {*} weight_var1-weight_var8;
array vars {*} var1-var8;
do i = 1 to dim(vars);
weights[i] = (vars[i] ne 0);
end;
run;
%macro weighted_var(n);
%do i = 1 to &n;
var var&i /weight = weight_var&i;
%end;
%mend weighted_var;
proc means data = xyz_v;
%weighted_var(8);
run;
This is less elegant than Chris J's solution for this specific problem, but it generalises slightly better to other situations where you want to apply different weightings to different variables in the same summary.
Can't you use a data statement?
data lala;
set xyz;
drop qty;
mean = 0;
qty = 0;
if(not missing(var1) and var1 ^= 0) then do;
mean + var1;
qty + 1;
end;
if(not missing(var2) and var2 ^= 0) then do;
mean + var2;
qty + 1;
end;
/* ... repeat to all variables ... */
if(not missing(var8) and var8 ^= 0) then do;
mean + var8;
qty + 1;
end;
mean = mean/qty;
run;
If you want to keep the mean in the same xyz dataset, just replace lala with xyz.