Hi,
When I run a proc means and export the data there is an option to append a specific statistic to the new summary variable name (i.e. _MAX, _MIN, _SUM etc.).
for example if I was taking a Sum of Total_Sales my output would come out as TOTAL_SALES_SUM.
Is it possible to add another standard prefix or suffix to all my variables in the procedure?
so in this case I'd like to add ACTIVE_ to the variable name as a prefix so it would look like: ACTIVE_TOTAL_SALES_SUM
thanks for your help.
You can always use the rename option. e.g.:
PROC MEANS DATA=sashelp.class
(rename=(height=active_height
age=active_age
weight=active_weight))
NOPRINT;
VAR active_age active_height active_weight;
OUTPUT OUT=want
mean =
median = / AUTONAME;
run;
A SAS support note shows a way to somewhat automate the task: http://support.sas.com/kb/34/568.html
Thanks for your reply Art.
I've been using the rename function but my dataset has ~200 variables that I would like to add the prefix too, so instead of writing it all out I was wondering if there is a quicker/easier way to append the prefix.
maybe a macro or some sort?
Did you look at the link I included in my last response? That is precisely what the note provides a method for accomplishing. http://support.sas.com/kb/34/568.html
Hi Art,
I looked at the link you provided. I learn something from you everyday. Thank you!
/* get the list of variable names in the dataset */
proc contents data=sashelp.class out=stuff(keep=name type) noprint;
run;
data temp;
set stuff;
length sumname $30.;
sumname=cats('active_',name,'_sum');
run;
/* create macro var with list of numeric variable names */
proc sql;
select (sumname)
into :outname separated by ' '
from temp
where type = 1;
select (name)
into :inname separated by ' '
from temp
where type = 1;
quit;
proc means data=sashelp.class;
var &inname;
output out=mymeans(keep=active:) sum= &outname;
run;
/* look at final output */
proc print data=&syslast; run;
You can always change it afterwards.
data temp;
set sashelp.class;
run;
proc summary data=temp;
var _numeric_;
output out=have(drop=_type_ _freq_) sum= mean= /autoname;
run;
*Add prefix;
data _null_;
set sashelp.vcolumn(keep=libname memname name where=(libname='WORK' and memname='HAVE')) end=last;
if _n_ eq 1 then call execute('proc datasets library=work nolist;modify have;rename ');
call execute(cats(name,'=','Active_',name));
if last then call execute(';quit;');
run;
*Add suffix;
data _null_;
set sashelp.vcolumn(keep=libname memname name where=(libname='WORK' and memname='HAVE')) end=last;
if _n_ eq 1 then call execute('proc datasets library=work nolist;modify have;rename ');
call execute(cats(name,'=',name,'_Active'));
if last then call execute(';quit;');
run;
Ksharp
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.