Can input function take a macro variable as character argument? - sas

SAS says: ERROR: INPUT function requires a character argument.
when I run the following code:
%let fyq0 = 000930 ;
%put &fyq0 ;
proc sql ;
create table check as
select *, input(&fyq0,yymmdd6.) as fyq0
from q ;
quit ;
I tried to have '&fyq0' instead of &fyq0 as the 1st argument for the -input- function as the following:
%let fyq0 = 000930 ;
%put &fyq0 ;
proc sql ;
create table check as
select *, input('&fyq0',yymmdd6.) as fyq0
from q ;
quit ;
Then SAS says: NOTE: Invalid argument to function INPUT. Missing values may be generated. And indeed, variable fyq0 returns a missing value.
I wonder what went wrong with my approaches and what is the correct way to go.

The macro processor just replaces the macro reference with the resolved text and then the generated text is interpreted as text. So when you tried.
input(&fyq0,yymmdd6.)
it was the same as if your code was
input(000930,yymmdd6.)
Even if that did run SAS would first have to convert the number 930 into a character string using the BEST12. format which would result in a string like
' 930'
and the input function would only read 6 of the leading spaces.
The macro processor does not process text inside of single quotes. So the INPUT function could not convert the five character string '&fyq0' to a valid date since the letters and ampersand are not valid digits.
You can use double quote characters to allow SAS to resolve the macro variable reference.
input("&fyq0",yymmdd6.)
Now the macro variable reference will resolve and code generated will be:
input("000930",yymmdd6.)

Related

SAS Function COUNTW - how to count word including NULL value when using customised delimiter?

Trying to use the function COUNTW to get the number of "words" within a string.
But this string may sometimes contain NULL value when it is comma delimited;
I found some custom macro that can do that, just wondering if I can achieve the same with in-built function, COUNTW for example.
For example:
%put %wordcount('abc, , ,def,,56j,type',DLM=%str(,));
The above is a custom function, and it returns 7
%put %sysfunc(countw('abc, , ,def,,56j,type',%str(,)));
COUNTW returns 6 because it does not counting the NULL between 2 commas (",,")
Very simple, just look at the online documentation! COUNTW has optional modifiers, one of which is M which tells the function to include consecutive delimiters as a word, not null.
The following code returns 7, not 6.
%put %sysfunc(countw('abc, , ,def,,56j,type',%str(,),M));

Tranwrd just one letter in SAS

How can i quote just one letter in sas?
%sysfunc(tranwrd(%quote(&string),%quote(T),%quote('Test')));
The Problem is, when the string has a 'T' and 'TR' that both get tranwrd to 'Test'
SAS Macro variables are always character. The arguments to macro functions are always character and generally won't require an extra layer of macro macro quoting, and definitely won't if the arguments are to be as literals.
Did you try this first ?
%let string = STACKOVERFLOW;
%let string_tweaked = %sysfunc(tranwrd(&string),T,Test);
%put NOTE: string_tweaked = &string_tweaked;
Do the macro values contain embedded single quotes ?
%let string = %str(S%'T%'ACKOVERFLOW);
%let string_tweaked = %qsysfunc(tranwrd(&string,'T','Test'));
%put NOTE: string_tweaked = &string_tweaked;
The second code sample is analogous to the following DATA step code (whose scope is different than that of the MACRO environment). DATA step string values are explicitly quoted, with either double quote (") or single quote (')
data _null_;
string = "S'T'ACKOVERFLOW";
string_tweaked = tranwrd(string,"'T'","'Test'");
put "NOTE: " string_tweaked=;
run;

Length of a SAS macro variable that contains quotes and commas

Let the sample macro variable be
%let temp="A","B","C";
How do you get the array length of this macro variable, which includes quotations and commas? For example, I would want to return length 3.
%let length_of_temp=%sysfunc(SOME_FUNC(&temp.));
%put length=length_of_temp;
LOG: length=3
Preferably I would want to do it using one established SAS function or line of code, not creating a new function for processing. Here is what I have attempted so far:
countw("&temp.",","): the quotes create an error when trying to convert it to a string.
NOTE: Line generated by the macro variable "TEMP". 4
""A","B","C"
NOTE 49-169: The meaning of an identifier after a quoted string might change in a future SAS
release. Inserting white space between a quoted string and the succeeding
identifier is recommended.
ERROR 388-185: Expecting an arithmetic operator.
countw(&temp.,",") and count(&temp.): typical error of function call has too many arguments
count((&temp.)) and dim((&temp.))
variations using %superq on the above
Use macro quoting on your macro variable value so that commas do not cause trouble for call to countw() function. Use the q and possibly the m optional third argument to the countw() function to let it know not to count delimiters that are inside the quotes.
%let temp="A1,a2","B","C";
%let count = %sysfunc(countw(%superq(temp),%str(,),mq));
If you want to calculate the count in a data step then instead of macro quoting you can use the symget() function to retrieve the value of the macro variable.
count = countw(symget('temp'),',','mq');
THis works for me:
%let temp="A","B","C";
%let count = %sysfunc(countw("&temp", ","));
%put Number of elements = &count.;
Results:
8002 %put Number of elements = &count.;
Number of elements = 3
The %quote function might be helpful here to mask the quotation marks:
%let count = %sysfunc(countw(%quote(&temp), ","));
%put Number of elements = &count.;

Append extra characters after a macro variable in SAS?

I have a simple macro where I am passing in a parameter but also want to append onto the macro. However, when I try to add the additional text it won't recognize the original macro variable. I have tried to convert the macro variable to a string first, append the extra text, then unquote it but can't find an appropriate concatenate function to use.
Here is my macro and what isn't working now, with the problem being &del_30 in the third line. The compiler is trying to interpret &del_30 as a macro, instead of &del_ by itself.
%macro plot_better_same_worse(title_, del_);
proc Sgplot data=ALL_TP_NORM_TBL;
SERIES X = asofdt Y = &del_30 /
MARKERS LINEATTRS = (THICKNESS = 2);
run;
%mend plot_better_same_worse;
I have also tried to do this instead: &&del_&30 but SAS tries to interpret &30 as a macro variable as well.
Macro variable names begin with & and end with ., or the first character illegal to be in a macro variable name (A-Z, 0-9, _).
So &del_.30 would resolve &del_ and then put 30 after it.

SAS numeric to character conversion?

When we convert a numeric to character , we should use a numeric format like the following
data test ;
prodID = 001 ;
result = put(prodID , 1.) ;
run ;
proc print ;
run ;
I also tried to use a character format $1. , and it also worked
data test ;
prodID = 001 ;
result = put(prodID , $1.) ; *I am using $1. here ;
run ;
proc print ;
run ;
Question is why did the second code work ? It was not supposed to work . Should we use a numeric or character format or it does not matter ?
You do get a warning with the second code:
WARNING: Variable prodID has already been defined as numeric.
That's because you are applying a character format to a numeric variable
But the result of the put function is always character.
To convert a character variable to a numeric variable, you use the INPUT() function (which uses informats).
newvar_num = INPUT(oldvar_char, informat)
The INPUT() function is similar to reading external data using the INPUT statement. The informat tells SAS how to read the data, and it should be read as numeric. When converting from character to numeric, the informat must be the type you are converting to, so numeric.
To convert a numeric variable to a character variable, you use the PUT() function (which uses formats).
newvar_char = PUT(oldvar_num, format)
The PUT() function is similar to writing out data using the PUT statement. The format tells SAS how to output or store the data. In the PUT() function, the format must be the same type as the source variable (oldvar_num), so numeric.
The PUT() function can also be used to convert a character variable to another character variable, with a character format. See examples A and C: https://blogs.sas.com/content/sgf/2015/05/01/converting-variable-types-do-i-use-put-or-input/.
The source variable of INPUT() function must always be character, the output can be character or numeric. The output of PUT() function is always character, the input can be character or numeric.
A good explanation of informats and formats can be found here: https://libguides.library.kent.edu/SAS/Informats-Formats.