SAS does not evaluate the content of %PUT - sas

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

Related

A better way to test if an macro variable is an valid SAS number or not

I am working on a SAS macro to validate if a macro variable is an valid SAS number or not. My solution is based on prxmacth() function:
%macro IsSASnumber(number);
%sysfunc(prxmatch(/^-?(?:\d+|\d*\.\d+)(?:e-?\d+)?|\.[a-z]?$/i,&number));
%mend;
There are several examples:
%put %IsSASnumber(123);
1
%put %IsSASnumber(1.23);
1
%put %IsSASnumber(-.12e-3);
1
%put %IsSASnumber(.N);
1
%put %IsSASnumber(.tryme);
0
My question is:
Is this regular expression covers all condition?
Is there a shorter or faster way to achieve this?
Ps: Assume the input is not empty.
If the goal is to support using the INPUT() function without generating error messages when the strings do not represent numbers then just use the ? or ?? modifiers to suppress the errors.
Since the INPUT() function does not care if the width used on the informat specification is larger then the length of the string being read just use the maximum width the informat supports. So just use:
number = input(variable,??32.);
You might also want to test the length of VARIABLE, the numeric informat can only handle strings up to 32 bytes long. You might want to remove any leading spaces.
if length(left(variable)) <= 32 then number=input(left(variable),??32.);
If you want strings like "N" or "X" to be treated as meaning the special missing values .N and .X then make sure to tell SAS that in advance by using the global MISSING statement. To support all 27 special missing values use a missing statement like this:
missing abcdefghijklmnopqrstuvwxyz_ ;
If you want to treat '.N' as meaning .N instead of . then you will need to test for that string. To test all of them you could use something like:
if missing(number) and length(variable)=2 and char(variable,1)='.'
then number=input(char(variable,2),??32.)
;
Note: make sure to use the name of an INFORMAT when using the INPUT() function. BEST is the name of a FORMAT (the name makes no sense as a name for an informat since there is only one way to represent a number as a number). If you use BEST as an INFORMAT SAS will just treat it as an alias for the normal numeric informat.
The %datatyp macro can determine all of these, but it fails at .N. You can simplify your use case this way:
%macro IsSASnumber(number);
%sysevalf(%datatyp(&number) = NUMERIC OR %sysfunc(prxmatch(/^\.[A-Z_]$|^\.$/i, &number)));
%mend;
This will match your numeric cases, and then you can match the . cases.

How to recall a numeric macro variable with a string prefix in SAS

I obtain an error in SAS while creating a new field in a dataset recalling a numeric macro variable. Here there's an example.
data input;
input cutoff_1 cutoff_2;
datalines;
30 50
;
data db;
input outstanding;
datalines;
1000.34
2000.45
3000.90
5000.98
8000.02
;
data _null_;
set input;
call symput("perc1",cutoff_1);
call symput("perc2",cutoff_2);
run;
proc univariate data=db noprint;
var outstanding;
output out=test
pctlpts = &perc1. &perc2.
pctlpre = P_;
run;
data test2;
set test;
p_&perc1._round=round(P_&perc1.,1);
p_&perc2._round=round(P_&perc2.,1);
run;
From the log it seems that the macros &perc. are solved, but that it's not possible to use those results (30, 50) to name a new variable in the dataset test2. What am I missing?
When you ask normal SAS code to use a numeric value in a place where a character value is required (both arguments to CALL SYMPUT() require character values) then SAS will convert the number into a string using the BEST12. If the value does not require all 12 characters it will be right aligned. So you created macro variables with leading spaces. The leading spaces make no difference for generating pctlpts= option values as extra spaces will not matter there. But having the leading spaces mean you are generating code like:
p_ 30_round=round(P_ 30,1);
You should use the modern (probably over 20 years old) CALL SYMPUTX() function instead. That function will remove leading/trailing spaces from the second argument when creating the macro variable. It also accepts a numeric value as the second argument converting the number into a character string using a format more like BEST32 instead of BEST12.
call symputx("perc1",cutoff_1);
call symputx("perc2",cutoff_2);
The only cases where you should ever use the ancient CALL SYMPUT() function is when you actually need to create macro variables that contain leading and/or trailing spaces.
Other solutions are to remove the spaces in the function call
call symput("perc1",strip(put(cutoff_1,best32.)));
or after generating the macro variables.
%let perc1=&perc1;
One possible solution is to use call symputx and not call symput. With call symputx the variables created are globally defined.

symputx won't let me store different macro variables with the same macro function

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.

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;