SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
Danglytics
Calcite | Level 5

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.

5 REPLIES 5
art297
Opal | Level 21

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

Danglytics
Calcite | Level 5

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?

art297
Opal | Level 21

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

Linlin
Lapis Lazuli | Level 10


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;

Ksharp
Super User

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

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 15550 views
  • 0 likes
  • 4 in conversation