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;
First place to check should always be the documentation.
CALL SYMPUTX(macro-variable, value <, 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.
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;
First place to check should always be the documentation.
CALL SYMPUTX(macro-variable, value <, 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.
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;
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
And if you're looking to bin variables by percentiles, PROC RANK is a better option IMO.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.