Can't execute correctly basic sas program - sas

I am a new learner of sas language. I know that this is easy for you. I just want to understand how does it work using macro variables. In fact , I just need to run a sas program correctly. I have tow marco variables
&x=12,20,40,77
&y=12,45,54,78
I just need to compute the max and the min of them.So I write this code:
%let &x=12 20 40 77;
%let &y=12 45 54 78;
%put max min (&&x);
%put max min (&&y);
It shows me this error saying:
Open code statement recursion detected
I tried to find solution but it doesn't work !how resolve it please?

To get the error you received you have probably confused SAS. The code you posted generates these errors:
WARNING: Apparent symbolic reference X not resolved.
ERROR: Expecting a variable name after %LET.
1 %let &x=12 20 40 77;
2 %let &y=12 45 54 78;
WARNING: Apparent symbolic reference Y not resolved.
ERROR: Expecting a variable name after %LET.
3 %put max min (&&x);
WARNING: Apparent symbolic reference X not resolved.
max min (&x)
4 %put max min (&&y);
WARNING: Apparent symbolic reference Y not resolved.
max min (&y)
In the first statement you are trying to create a macro variable by using the value of the macro variable X as the name of the macro variable. But X was never defined so &X just resolved to &X. Since & is not a valid character to use in the name of a macro variable you end up with an invalidly formed %let statement.
If we fix the first two statements so that they generate valid %LET statements then the code runs. It is not clear what you wanted it to do but it works as SAS intended.
5 %let x=12 20 40 77;
6 %let y=12 45 54 78;
7 %put max min (&&x);
max min (12 20 40 77)
8 %put max min (&&y);
max min (12 45 54 78)
The extra & in the %put statements just make SAS have to take two passes to resolve the macro variable references. The first pass will resolve && to & and trigger the macro processor to make another pass. Then the second pass will properly resolve the resulting &x to the string 12 20 40 77.
It is best to work with numbers using SAS code and not macro code. If you want to find the max of a list of numbers using the MAX() function. Normally you can use the OF keyword in data steps when you have values separated by spaces but that only works with variable names, not literal values. So change the spaces to commas.
9 %put &=x ;
X=12 20 40 77
10 data _null_;
11 max=max(%sysfunc(translate(&x,%str(,),%str( ))));
12 put max=;
13 run;
max=77
If you really want to stay in macro language only then use %SYSFUNC() function again to call the MAX() function.
14 %put max=%sysfunc(max(%sysfunc(translate(&x,%str(,),%str( )))));
max=77

Refer to this link
I tried it around like this.
%let val1=17, 24, 35, 76;
%let val2=87, 32, 45, 6;
data _null_;
max=%sysfunc(max(&val1, &val2));
min=%sysfunc(min(&val1, &val2));
put max= min=;
run;
I hope it helps.

Related

SAS MACRO how to assign MACRO to date9

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;

SAS does not evaluate the content of %PUT

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)).

SAS macro write text file with char and num parameter

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.));

SAS macro variable issue- 'File Name value exceeds maximum length'

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;

SAS indirect macro processing [duplicate]

This question already has an answer here:
sas MACRO ampersand
(1 answer)
Closed 8 years ago.
I have a question on an exam paper which asks what is read in the SAS log
%let test=one;
%let one=two;
%let two=three;
%let three=last;
%put what displays is &&&&&test;
I was very surprised to find the answer was: two as I would have thought that this reference would fully resolve to last. SAS also agrees with the answer to be two.
Can anyone please explain how SAS arrives at the answer two as all theory notes I have read suggests that the macro processor should do the following
scan1 &&&&&test - > &&&&test ( i.e && resolves to & and tells processor to continue to scan from right to left)
scan2 &&&&test - > &&&one
scan3 &&&one - > &&two
scan4 &&two - > &three
scan5 &three - > last
Using the symbolgen option can help see what is happening in the log:
1 options symbolgen;
2 %let test=one;
3 %let one=two;
4 %let two=three;
5 %let three=last;
6
7 %put what displays is &&&&&test;
SYMBOLGEN: && resolves to &.
SYMBOLGEN: && resolves to &.
SYMBOLGEN: Macro variable TEST resolves to one
SYMBOLGEN: && resolves to &.
SYMBOLGEN: Macro variable ONE resolves to two
what displays is two
Going left to right and using brackets to show the tokens:
&&&&&test
(&&)(&&)(&test)
(&) (&) (one)
&&one
(&&)(one)
(&)(one)
&one
two