BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
hellohere
Pyrite | Level 9

I have the macro below to assign percentiles to macro variable.

It works, %pctvar(sashelp.cars, Horsepower, hp); 

BUT the macro variables are LOCAL only within the macro. How to make them global?! Thanks, 

 

%macro pctvar(ds, var, note);
proc univariate data=&ds. noprint;
var &var.;
output pctlpre=p_ pctlpts= 5 to 95 by 5;
run;
proc print; run;
data _null_;
set;
array p_ p_:;
do over p_;
call symputx(vname(p_),p_); /*only local, not global*/
end;
stop;
run;quit;
%put "p_5=&p_5. p_50=&p_50. p_95=&p_95.";
%mend;

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

First place to check should always be the documentation

 

 

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.
 
So add, g to your call symput function. 
 
call symputx(vname(p_), p_, 'G' );
 

@hellohere wrote:

I have the macro below to assign percentiles to macro variable.

It works, %pctvar(sashelp.cars, Horsepower, hp); 

BUT the macro variables are LOCAL only within the macro. How to make them global?! Thanks, 

 

%macro pctvar(ds, var, note);
proc univariate data=&ds. noprint;
var &var.;
output pctlpre=p_ pctlpts= 5 to 95 by 5;
run;
proc print; run;
data _null_;
set;
array p_ p_:;
do over p_;
call symputx(vname(p_),p_); /*only local, not global*/
end;
stop;
run;quit;
%put "p_5=&p_5. p_50=&p_50. p_95=&p_95.";
%mend;



View solution in original post

3 REPLIES 3
Reeza
Super User

First place to check should always be the documentation

 

 

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.
 
So add, g to your call symput function. 
 
call symputx(vname(p_), p_, 'G' );
 

@hellohere wrote:

I have the macro below to assign percentiles to macro variable.

It works, %pctvar(sashelp.cars, Horsepower, hp); 

BUT the macro variables are LOCAL only within the macro. How to make them global?! Thanks, 

 

%macro pctvar(ds, var, note);
proc univariate data=&ds. noprint;
var &var.;
output pctlpre=p_ pctlpts= 5 to 95 by 5;
run;
proc print; run;
data _null_;
set;
array p_ p_:;
do over p_;
call symputx(vname(p_),p_); /*only local, not global*/
end;
stop;
run;quit;
%put "p_5=&p_5. p_50=&p_50. p_95=&p_95.";
%mend;



hellohere
Pyrite | Level 9

Reeza:

 

How to do Proc Univariate with one BY Var here?!

Say byvar has value 1,2,3,4, and to systematically save varx(one numeric) percentile cut into macro variables

p_by_1_varx_10/20/30/....

 

Thanks

Reeza
Super User

And if you're looking to bin variables by percentiles, PROC RANK is a better option IMO. 

 

 

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
  • 3 replies
  • 1329 views
  • 2 likes
  • 2 in conversation