DATA Step, Macro, Functions and more

Variable Autonaming with prefix or suffix

Reply
Contributor
Posts: 66

Variable Autonaming with prefix or suffix

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.

PROC Star
Posts: 7,363

Variable Autonaming with prefix or suffix

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

Contributor
Posts: 66

Variable Autonaming with prefix or suffix

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?

PROC Star
Posts: 7,363

Variable Autonaming with prefix or suffix

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

Super Contributor
Posts: 1,636

Re: Variable Autonaming with prefix or suffix


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 Smiley Surprisedutname 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=activeSmiley Happy sum= &outname;                                 

run;                                                                

                                                                    

/* look at final output */ 

proc print data=&syslast; run;

Super User
Posts: 9,681

Variable Autonaming with prefix or suffix

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

Ask a Question
Discussion stats
  • 5 replies
  • 5591 views
  • 0 likes
  • 4 in conversation