Hello,
I saw this code that :
a- Define a macro varaible called vector with concatenated values (with comma between the values)
b- Define a macro varaible called n that count number of arguments in macro varaible vector (3 in this example)
I want to ask please about the code:
Why are there 3 arguments to CALL SYMPUTX? What is the meaning of 'g'? Why do we use PUT fucction with best. format?
%let vector = type1,type2,type3;
data _null_;
call symputx('n',put(countw("&vector.",','),best.),'g');
run;
%put &n.;
You don't need to ask in this forum what an option does. The meaning of "g" is in the documentation for CALL SYMPUTX, please look it up.
Here's a case where you can answer your question yourself. Try running the code WITHOUT the PUT function. What happens? Does that answer the question?
First and second questions, the documentation is pretty straightforward here:
CALL SYMPUTX(macro-variable, value <, symbol-table>);
..
specifies a character constant, variable, or expression. The value of symbol-table is not case sensitive. The first non-blank character in symbol-table specifies the symbol table in which to store the macro variable. The following values are valid as the first non-blank character in symbol-table:
specifies that the macro variable is stored in the global symbol table, even if the local symbol table exists.
specifies that the macro variable is stored in the most local symbol table that exists, which will be the global symbol table, if used outside a macro.
specifies that if the macro variable exists in any symbol table, CALL SYMPUTX uses the version in the most local symbol table in which it exists. If the macro variable does not exist, CALL SYMPUTX stores the variable in the most local symbol table.
@Ronein wrote:
Hello,
I saw this code that :
a- Define a macro varaible called vector with concatenated values (with comma between the values)
b- Define a macro varaible called n that count number of arguments in macro varaible vector (3 in this example)
I want to ask please about the code:
Why are there 3 arguments to CALL SYMPUTX? What is the meaning of 'g'? Why do we use PUT fucction with best. format?
%let vector = type1,type2,type3; data _null_; call symputx('n,put(countw("&vector.",','),best.),'g'); run; %put &n.;
The G value for the third option forces the generated macro variable into the GLOBAL macro scope, even if you are running the code inside a macro.
The PUT() function call is not needed with CALL SYMPUTX(), especially if you are just going to use the BEST format, because CALL SYMPUTX() will automatically convert numbers in the second argument into strings to store in the macro variable.
I suspect the code used to use the older CALL SYMPUT() function that did not support the third parameter. So when they switched to to using CALL SYMPUTX() so they could insure the macro variable value was written into the global scope then just left the PUT() function in the code. (Or perhaps is was just an older CODER instead of older CODE).
Nowadays you can call COUNTW() in macro code if you want.
%let vector = type1,type2,type3;
%global n;
%let n=%sysfunc(countw(%quote(&vector),%str(,)));
%put &=n;
Why the commas? Where is the value used in the code? Are those variable names that you need to include in SQL code? Because if you want to use a list of variables in normal SAS code then use a space between the names. Plus using space instead of comma means it is easier to use the value with macro calls.
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.