I try to reassign macro to new macro, which I want to create date9.
so my code is this
%Let CALYEAR =2020;
%let AYEAR =%SYSEVALF(&CALYEAR-1);
FALLSTART_ONE=%sysfunc(15AUG&AYEAR,date9.);
so I assume Macro variable "AYEAR" is 2019 and I want to create macro variable "FALLSTART_ONE" =15AUG2019
but error is
212 FALLSTART_ONE=%sysfunc(15AUG&AYEAR,date9.);
-------------
180
ERROR: Function name missing in %SYSFUNC or %QSYSFUNC macro function reference.
How should I fix it?
Thanks
You need a %let. You also do not need to enclose it in %sysfunc(). You can build the string as-is.
%let FALLSTART_ONE=05AUG&AYEAR;
Related
I'm trying to write a macro function that gets attributes from a data set, and then stores them as a macro variable. I want to write this macro function in such a way that it can be used for multiple data sets, and multiple macro variables.
What's wrong with this:
%macro ExtractACell(dataset, storage_var, rownum=1, var_name=Make);
data _null_;
set &dataset. (obs=&rownum. firstobs=&rownum. keep = &var_name.);
call symputx(&storage_var., &var_name., "G");
stop;
run;
%mend ExtractACell;
Whenever I try to run that I get notes like this:
NOTE: The quoted string currently being processed has become more than 262 bytes long. You might have unbalanced quotation marks.
NOTE: The quoted string currently being processed has become more than 262 bytes long. You might have unbalanced quotation marks.
27 %let SASWORKLOCATION="%sysfunc(getoption(work))/";
Does symputx have rules against passing in macro variables that represent names of macro variables?
Make sure you are generating valid SAS code when your macro variables are expanded. In particular this statement:
call symputx(&storage_var., &var_name., "G");
Would require you to call the macro with quotes around the name of the macro variable you want it to create. Like this:
%ExtractACell(dataset=sashelp.cars, storage_var="mymvar", rownum=1, var_name=Make);
It might be simpler to code the macro using this statement.
call symputx("&storage_var", &var_name., "G");
Then you could call without the quotes around the macro variable name:
%ExtractACell(dataset=sashelp.cars, storage_var=mymvar, rownum=1, var_name=Make);
PS Your whole process is probably messed up if you need that macro.
I use this bit outside of any DATA step.
%let sth = 20191111;
%let sthelse=SUBSTR(INPUT(&sth.,12.),1,4);
%put &sthelse.;
It does not yield '2019', which I would expect but rather
SUBSTR(INPUT(20191111,12.),1,4)
What goes wrong here?
Use the macro function %SUBSTR to extract a portion of its argument. Remember, macro values are only character values (not to be confused with data step character variables and values) and have no explicit numeric value, even when the macro value is comprised of all digits.
%let sth = 20191111;
%let first4 = %substr(&sth,1,4);
You are not understanding how the macro pre-processor works. It is just a text replacement tool. It looks for two trigger character, % and & to see where it needs to do work. You can see both at work in your statement:
%let sthelse=SUBSTR(INPUT(&sth.,12.),1,4);
So the % will trigger the macro processor and it will recognize %let as a macro statement. Then the & trigger will cause it to treat &sth. as a macro variable reference to be replaced. So it replaces that and you end up with this statement.
%let sthelse=SUBSTR(INPUT(20191111,12.),1,4);
Since there are no more macro triggers SAS happily stores that text into the macro variable sthelse.
You also seem confused about how numbers and character strings work. If you write this code:
data x;
x=INPUT("20191111",12.);
run;
You would be asking SAS to convert the string 20191111 into the number 20,191,111. So X would be a numeric variable. So it would have the same effect as if you ran this statement instead.
x=20191111;
Now if you then ask SAS to try to use the SUBSTR() function on that NUMERIC value by doing this:
y=substr(x,1,4);
SAS will need to first convert X into a character string. It will happily do that for you, but it will use the BEST12. format to make the conversion. So your number 20,191,111 will become the string
20191111
So it will have 4 leading spaces. Then if you take the first four characters you end up with four spaces.
Did you intend to run this code instead?
%let sth = 20191111;
%let sthelse=%substr(&sth,1,4);
That will use the MACRO function %SUBSTR() to take the first four characters of the string you have given it. Since you gave it the string 20191111 (to the macro processor everything is a string, no quotes needed) the result will be the string 2019.
SUBSTR is datastep function.
Try to use it into macro function:
%let sth = 20191111;
%let sthelse=%sysfunc(SUBSTR(%sysfunc(INPUTn(&sth.,12.)),1,4));
%put &sthelse.;
There isn't macro function named substr and input, this function is "data step functions". You should use %sysfunc statement. And macro variables stores as text, so you shouldn't use input function on it.
%let sth = 20191111;
%let sthelse=%sysfunc(SUBSTR(&sth.,1,4));
%put &=sthelse.;
STHELSE=2019
UPDATE(thanks #Tom):
There is macro function %substr, so you can use %SUBSTR(&sth.,1,4) instead of %sysfunc(SUBSTR(&sth.,1,4)).
I am trying to write a macro that creates a text file using parameter value. I understand all SAS parameters are passed as text, so i need to convert the text to numeric. Using INPUT for this but still getting a syntax error. Appreciate the help. Thank you.
code:
%macro test(n_var);
data _null_;
file"c:/temp/test.txt" TERMSTR=crlf;
put ;
put "(numeric variable passed = "input(&n_var,8.)")";
put ;
run;
%mend;
%test(n_var=100);
Log:
SYMBOLGEN: Macro variable N_VAR resolves to 100
NOTE: Line generated by the macro variable "N_VAR".
39 100
___
22
76
MPRINT(TEST): put "(numeric variable passed = "input(100,8.)")";
MPRINT(TEST): put ;
MPRINT(TEST): run;
ERROR 22-322: Syntax error, expecting one of the following: a name, arrayname, _ALL_, _CHARACTER_, _CHAR_, _NUMERIC_.
ERROR 76-322: Syntax error, statement will be ignored.
All SAS macro symbols (aka variables) are text. SAS macro parameters are text.
Your use case probably does not need to convert text to numeric.
Consider:
%let x = 100;
data _null_;
my_num_var = &x;
run;
The resolution of the macro variable (or perhaps better understood as 'symbol') are the letters 1 0 0, but with respect to text to be interpreted as SAS code. The data step compiler infers my_num_var is numeric from the line it sees as
my_num_var = 100;
There are some use cases where you may want to test that a macro parameter can be interpreted as a numeric value. Such use cases are probably beyond your needs at this time.
The INPUT function is one of those special DATA Step functions that is not available for use in SAS macro via the %sysfunc function. When you must 'input' a value in a 'pure' manner outside a DATA step, you will want to invoke the INPUTN or INPUTC functions via %sysfunc
%let evaluatedRepresentation = %sysfunc(inputn(&x,best8.));
The numeric evaluation of the inputn is converted to text and assigned to symbol evaluatedRepresentation.
If you are not in control of the callers to you macros in which you do ampersand evaluations the safer approach is to evaluate via SUPERQ to break code injections and other anamolies
%let evaluatedRepresentation = %sysfunc(inputn(%superq(x),best8.));
I'm trying to leverage macro variables (assigned via %LET) for PROC IMPORTS but it's causing the file name to exceed 201 characters.
(ERROR: File Name value exceeds maximum length of 201 characters)
Is there a different way to define variables so that they are passed as values instead of functions?
My old datafile string ends
DATAFILE="...Files_Submitted\201612\Reconcile\Q42016 Principal balances.xls"
I have changed it to
DATAFILE="...Files_Submitted\&yrmm.\Reconcile\&quarter. Principal balances.xls"
Using the following variables.
%LET EOLM= INTNX('MONTH',today(),-&MonthsAgo.);
%LET yrmm= COMPRESS(year(&EOLM.)||PUT(month(&EOLM.),z2.));
%LET qtr = COMPRESS(year(&EOLM.)||COMPRESS('Q'||CEIL(month(&EOLM.)/3)));
Thanks in advance for any/all assistance.
You're not actually passing things the right way here - you're mixing two different things, which is causing your issue.
You can either resolve the results of these functions in your macro variables, OR you can store the function calls and treat this as if you'd typed the functions into the data step - so use CATS or something to combine them.
As it is, you're ending up with a filename like "\\path\to\COMPRESS(INTNX(MONTH...", not with the results of those functions, hence your problem.
So, one option:
DATAFILE= cats("...Files_Submitted\",&yrmm.,"\Reconcile\",&quarter.,"Principal balances.xls";
That would let the functions provide their values as you expect.
The other option is to use %SYSFUNC to ask the values to be resolved in the macro variables. This is the more common way, though certainly neither is really specifically better for all purposes.
%LET EOLM= %sysfunc(INTNX(MONTH,%sysfunc(today()),-&MonthsAgo.));
And similar for the other two. Note that I remove the quotes around MONTH as quotes are not used in %SYSFUNC calls (unless you want to use quote characters themselves, but not as string delimiters).
%LET yrmm= %sysfunc(year(&EOLM.))%sysfunc(month(&EOLM.),z2.);
Note here I put the format in the SYSFUNC call directly; also note that we do not use concatenation characters in macro variables (they just produce text) and typically you don't need to use COMPRESS (though not always).
%LET qtr = %sysfunc(year(&EOLM.))Q%sysfunc(CEIL(%sysevalf(%sysfunc(month(&EOLM.))/3)));
Here we use %SYSEVALF to do the math (normally you can't have noninteger math in macro syntax). We also remove quotes from Q and just place it in line.
Putting it all together:
%let monthsAgo = 3;
%LET EOLM= %sysfunc(INTNX(MONTH,%sysfunc(today()),-&MonthsAgo.));
%put &=EOLM;
EOLM=20789
%LET yrmm= %sysfunc(year(&EOLM.))%sysfunc(month(&EOLM.),z2.);
%put &=yrmm;
YRMM=201612
%LET qtr = %sysfunc(year(&EOLM.))Q%sysfunc(CEIL(%sysevalf(%sysfunc(month(&EOLM.))/3)));
%put &=qtr;
QTR=2016Q4
Of course more easy might have been to use formats for yrmm/qtr...
%let yrmm = %sysfunc(putn(&eolm.,yymmn6.));
%let qtr = %sysfunc(putn(&eolm.,yyq6.));
%put &=yrmm &=qtr;
Or even (and this might be getting a bit cute) removing the %SYSFUNC from &EOLM and letting the %SYSFUNC format option handle the formatting. Note here EOLM does not store a number, but stores the text you see on the screen, and the number doesn't get resolved until YRMM or QTR is defined.
%LET EOLM= INTNX(MONTH,%sysfunc(today()),-&MonthsAgo.);
%let yrmm = %sysfunc(&eolm.,yymmn6.);
%let qtr = %sysfunc(&eolm.,yyq6.);
%put &=yrmm &=qtr;
I would like to "shortcut" this function of a macro variable
intnx('YEAR',"&starting_year"D,-5,'S');
with another macro variable. Is it possible ?
something like:
%let start = intnx('YEAR',"&starting_year"D,-5,'S');
Of course this code-line doesn't work.
You can use %SYSFUNC() to call functions in macro code. You can use the optional format specification to control how the results is converted to the text that is stored in the macro variable.
So if you start with macro variable in DATE. format you could generate the date that was 5 years before that date like this:
%let start=%sysfunc(intnx(year,"&starting_date"d,-5,s),date9);
This would convert 20MAR1999 to 20MAR1994. Or it would convert 29FEB04 to 28FEB1999.
%sysfunc lets you call regular SAS functions in the macro language. Perhaps something like (untested) :
%let start = %sysfunc(intnx(YEAR,"&starting_year"D,-5,S));