I have two numeric variables, date1 and date2, which are both currently set to 20170502.
In my macro, I want to send an email if the two variables are equal. This is my code:
%let date1 = input(put(intnx('day', today(),-3),yymmddn8.),8.);
%macro EMAIL;
filename INSRNC email;
data _null_;
%if &date1. = &date2. %then %do;
file INSRNC to="email#address"
subject= "test";
put "email message";
%end;
run;
%mend;
%EMAIL;
I am getting the error message: "Required operator not found in expression"
Am I using the wrong operator?
Thanks
If these are defined macro variables, then this should work as you posted it. I would recommend making a modification; you should pass date1 and date2 as parameters, even if they are just passed as is, due to the principle of encapsulation. But what you posted would work, just based on global macro variables.
%let date1=20140101;
%let date2=20140101;
%macro EMAIL(date1=, date2=);
filename INSRNC email;
data _null_;
%if &date1. = &date2. %then %do;
*file INSRNC to="email#address"
subject= "test";
put "email message";
%end;
run;
%mend;
%EMAIL(date1=&date1., date2=&date2.);
If you're getting required operator not found in expression then your macro variables are not correctly defined and may have a character in them that is making this fail to read properly. Use OPTIONS MPRINT SYMBOLGEN; to see why.
#Joe gave a perfect explanation on how to define macros. Passing the macros as parameters makes it far easy. However, looking at your comment on how date1 is getting resolved, please see the below
date1 = %sysfunc(input(put(intnx('day', today(),-3),yymmddn8.),8.));
Kindly read through the importance of %Sysfunc in resolving macros to have a better understanding.
Related
so I have a code like below
%let THIS_YEAR=2020;
%macro programall;
%do i = 2016 %to &THIS_YEAR;
%let num2 =%eval(&i-2000);
%let xxx= CAT("MP",&num2);
data t_&i.;
set table1;
where GROUP in ("&xxx");
run;
%end;
for example
when i=2016
num2 = 2016-2000;
num2 = 16;
and try to concatenate with "MP", so it should create xxx=MP16.
and try to use in where statement.
but it is causing error.
how can I create Macro Variable like "MP16"correctly, so I can use it in where clause?
Thanks
You cannot use functions in macro code, they are just treated as any other text to the macro processor. But there is no need to use a function to concatenate text in macro code. Just expand the macro variable where you want to use the text it contains.
%let xxx= MP&num2 ;
Macro variables are just text (not strings, text as in the thing you type in). So to concatenate macro variables, just put them next to each other.
%let var1=Banana;
%let var2=Pepper;
%let var3=&var1. &var2.;
%put &=var3;
You don't actually have to use the third variable of course, you could just use "&var1. &var2." or whatever in your code directly.
Try
%let THIS_YEAR=2020;
%macro programall;
%local year;
%do year = 2016 %to &THIS_YEAR;
data t_&year.;
set table1;
where GROUP in ("MP%eval(&year-2000)");
run;
%end;
%mend;
options mprint;
%programall
I am getting a generic 'Statement not valid or out of order' message with the below:
%macro test;
data _null_;
%if %sysfunc(fileexist("C:\report_201809.xlsx")) = 1 %then %do;
rc=%sysfunc(rename("C:\report_201809.xlsx",
"C:\report_201809.xlsx"_old.xlsx",'file'));
%end;
%mend;
%test;
The code below should get you what you need. While you can use %if statements in a data step you generally won't need to. I'm guessing the error is coming from the %sysfunc function around the fileexist and rename functions. %sysfunc allows you to call data step functions outside of a data step so it is not needed here.
%macro test;
data _null_;
if fileexist("C:\file.txt") then do;
rc = rename("C:\file.txt", "C:\file2.txt", 'file');
end;
run;
%mend;
Alternatively, you could use an X Command that allows you to execute Windows commands. You could replace the rename function with the following statement.
x move C:\file.txt C:\file2.txt;
Remove the DATA _NULL_ or proceed per #J_Lard.
Macro arguments used in %sysfunc invoked function calls are implicitly quoted and do not need additional ' or "
%macro test;
%local rc;
%if %sysfunc(fileexist(C:\report_201809.xlsx)) = 1 %then %do;
%let rc = %sysfunc(rename(C:\report_201809.xlsx,C:\report_201809_old.xlsx,file));
%end;
%test;
You original code may have worked (by way of non-obvious side effect) if the filename "C:\report_201809.xlsx"_old.xlsx" (having an extraneous ") was corrected to "C:\report_201809_old.xlsx"
I'm using SYSPBUFF to pass through various numbers of parameters into a macro. Specifically, I am passing through a list of states. One of the states being used is Oregon or "OR" and that one state is causing me error.
I get the error "ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was: &ST^=
ERROR: The condition in the %DO %WHILE loop, &ST^=, yielded an invalid or missing value, . The macro will stop executing.
I've used all the various quoting masks to try to resolve this issue but none of it is working.
&STATES includes the following states:
AK,AZ,CA,HI,ID,NV,OR,WA
Here is my current code:
RSUBMIT;
PROC SQL;
connect to oracle
(path=DW user=&USER pw=&PW);
%macro DTCNT() / parmbuff;
%let i=1;
%let ST=%scan(&SYSPBUFF,&I);
%do %while (&ST^=);
CREATE TABLE MD_&ST._IP_ADJDT_CNTS_S1 AS
select *
from connection to oracle
(SELECT adjudication_date,
count (*) as LINE_CNT
from MD_r&NUM..&ST._IP_hdr_f
group by adjudication_date
order by adjudication_date);
%let i=%eval(&I+1);
%let ST=%scan(&SYSPBUFF,&I);
%end;
%mend DTCNT;
%DTCNT(&STATES);
disconnect from oracle;
QUIT;
ENDRSUBMIT;
Any assistance would be greatly appreciated.
Thanks in advance.
The issue here is Oregon. Its abbreviation is OR, which is also a reserved word (oops!). Remember the macro language is just text that is then parsed like normal - so when it finds &ST and translates to OR, it sees that as
%do %while (or ^= )
which causes it to get confused since it doesn't see anything to use with or.
You can use macro quoting here to cause SAS not to treat it like the boolean operator. %SUPERQ is the goto one for me, but a few of them should work.
Here's an example. I added some extra stuff to scan also to handle the parens.
%let states=AK,AZ,CA,HI,ID,NV,OR,WA;
%macro DTCNT() / parmbuff;
%let i=1;
%put &=syspbuff.;
%let ST=%scan(&SYSPBUFF,&I,%str(%(%),));
%put &=st.;
%do %while (%superq(ST)^=);
%put &=st;
%let i=%eval(&i.+1);
%let ST=%scan(&SYSPBUFF,&I,%str(%(%),));
%end;
%mend DTCNT;
%DTCNT(&STATES);
There is a great paper on how to test for empty macro variables, Chang Chung's Is This Macro Parameter Blank.
For your simple program I find it much easier to just use %length() to test for empty macro variables.
%do %while (%length(&ST));
I am wondering how I can set a macro-name from a variable.
Like this:
%Macro test(name);
...
%Macro new_&name;
...
%Mend;
...
%Mend test
Or if this is not possible:
%macro one(name);
%let mname=&name;
%mend one;
%macro two_&name;
...
%mend;
Any ideas? Many thanks.
First thing that pops into my mind is to use a temporary fileref to build your macros. Then include that fileref.
I think this does what you are looking for:
%macro test(x,file);
data _null_;
file &file;
format outStr $2000.;
outStr = ('%macro test_' || strip("&x") || "();");
put outStr;
put '%put hello world;';
outStr = '%put Passed in value is x:' || "&x and file: &file;";
put outStr;
put "proc means data=sashelp.class(obs=&x) mean; var age; run;";
put '%mend;';
run;
%include &file;
%mend;
filename tempfile temp;
%test(2,tempfile);
%test_2;
filename tempfile clear;
Yes you can do such a thing:
%macro macroFunc();
%put hi there;
%mend;
%macro macroCall(macroName);
%¯oName.();
%mend;
%mcr2(macroFunc);
But I'm really curious in what context this makes sense.
Seems like it will in no time result into a coding mess.
I never knew that you could not use a variable in a %MACRO statement...but that appears to be the case. As it says in the SAS documentation (http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/viewer.htm#macro-stmt.htm) "you cannot use a text expression to generate a macro name in a %MACRO statement."
My next thought was that you might be able to create the %MACRO statement as a variable, but I couldn't find a way to mask %MACRO in the creation of the variable.
I finally figured out a work around, but it is likely not the best way to do this (and it may not work for what you're trying to do). I found that I could compile the macro statement in a data step. Unfortunately though, I could only run the macro from the variable when the entire macro code (from the %MACRO to the %MEND statement) was saved in the variable. See the code below.
%MACRO test(name);
data test;
*COMPILE MACRO STATEMENT;
pct=%nrstr('%');
name="new_&name";
beginning=pct||'MACRO '||strip(name)||'();';
*CODE TO BE INSIDE MACRO;
/*Note: SAS will encounter errors if you try to assign text containing macro
functions (e.g., %PUT, %IF, etc.) to a variable. To get around this, you must
put hide the % in the following syntax, %nrstr('%'), and concatenate/join the
syntax with the rest of the string */
code=pct||'PUT HELLO!;';
*COMPILE MEND STATEMENT;
end=pct||'MEND;';
call symput('MacroStatement',beginning||code||end); *Output var containing macro;
call symput('Execute',pct||strip(name)||';'); *Output var containing statement to run macro;
output;
run;
&MacroStatement
&Execute
%MEND;
%test(name1);
options mprint mlogic symbolgen nospool;
%let definition=abc;
%let mdef=macro &definition.;
%&mdef.;
%put TEST;
%mend;
%abc;
I want to create a SAS macro which takes a literal date (eg. '31may2011'd) as parameter. Inside the macro I want to transform this into a SAS date value (eg. 18778).
%macro transLiteralDate2Value(literal=);
%put literal = &literal.;
%put sasdatavalue = ???; /* how to calculate this value ? */
%mend;
%transLiteralDate2Value(literal='31may2011'd);
Is the are elegant way to achieve this? Of course I could do this by parsing the literal string, but I think there must be a better way.
I use SAS 9.1.3
This will work inside or outside of a macro. Don't forget %sysfunc() has a handy optional second parameter which will let you format the output value.
%let report_date = %sysfunc(sum('01JAN2011'd),best.);
or
%let report_date = %sysfunc(putn('01JAN2011'd,best.));
Cheers
Rob
You can do it using the %sysfunc macro function.
%macro transLiteralDate2Value(literal=);
%put literal = &literal.;
%put sasdatavalue = %sysfunc(putn(&literal.,8.));
%mend;
%transLiteralDate2Value(literal='31may2011'd);
It is handy to have a pair of simple conversion macros like mine below. See also my sas-l posting.
%macro date2num(date, informat=anydtdte.);
%*-- strip quotations and postfix d from date literal if any. --*;
%*-- quotations are intentionally doubled to prevent unmatched error --*;
%let date=%sysfunc(prxchange(s/[''""]d?//i,-1,&date));
%sysfunc(inputn(&date,&informat))
%mend date2num;
%macro num2date(num, format=date10., literal=1);
%local n;
%let n = %sysfunc(putn(&num,&format));
%if &literal %then "&n"d; %else &n;
%mend num2date;