BookmarkSubscribeRSS Feed
Ronein
Meteorite | Level 14

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.;
4 REPLIES 4
PaigeMiller
Diamond | Level 26

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?

--
Paige Miller
Reeza
Super User

First and second questions, the documentation is pretty straightforward here:

 

CALL SYMPUTX(macro-variablevalue <, symbol-table>);

..

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:

G

specifies that the macro variable is stored in the global symbol table, even if the local symbol table exists.

L

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.

F

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.

Note: If you omit symbol-table, leave it blank or use any other symbol other than G, L, or F. CALL SYMPUTX stores the macro variable in the same symbol table as does the CALL SYMPUT routine.
 
To answer your third question - try it with and without the BEST function and see what happens...except the code you posted doesn't work at all so you may want to revisit that first.
 
 
 

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


 
 
Tom
Super User Tom
Super User

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

Tom
Super User Tom
Super User

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.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 731 views
  • 0 likes
  • 4 in conversation