REN_STEM

Rename variables

If you want to use REN_STEM, feel free to download it here:

Download ren_stem.sas

 

If you are not prompted to "Save To Disk", then right-click the link and choose "Save Link Target As..."  Otherwise, you will need to save the web page to your computer.  Make sure you save the ren_stem.sas file as a plain text file not an htm/html file.

 

Disclaimer:  There is no warranty on this software either expressed or implied.  This program is released under the terms and conditions of GNU General Public License.

 

About REN_STEM

Programmer:  Dan Blanchette
The Carolina Population Center
The University of North Carolina
Chapel Hill, NC  USA

Date:  27Apr2004
Last updated:  25Feb2009


Rename a list of variables by adding a specified prefix or suffix to the variable names

 

%ren_stem( dataset_name , variable list , constant , options );

 

 

Description

The REN_STEM SAS macro renames the selected variables by adding the specified prefix or suffix to the variable name.  For example, say you want to have all of your "wave 1" variables start with "w1_".  You could manually type:
   rename height = w1_height;
   rename weight = w1_weight;
   rename income = w1_income;
    . . .
Or you could use REN_STEM to do it for you:
   ren_stem(myData , _all_ not(id) , w1_ , before);

 

 

Options

NOTE:  The parameters have to be specified in the order of this list.
Parameters
Explanation
(1st)

dataset_name

Full name of dataset (include libref if necessary) in which variables are to be renamed. 

If no dataset name is provided then the last dataset used is processed; but the call to REN_STEM must start with a comma since the first parameter is the dataset name:
   %ren_stem( , height weight income , w1_ , before);


(2nd)

variable_list

Can be of the following different valid SAS variable list styles:

gender race - the two variables gender and race

var1-var5 - var1 var2 var3 var4 var5

age--sampwt - could be:  age gender weight psu sampwt

age-numeric-sampwt - only the numeric variables between age and sampwt

age-character-sampwt - only the character variables between age and sampwt

_all_ - all variables in dataset

_numeric_ - all numeric variables in dataset

_character_ - all character variables in dataset

va: - all variables in dataset that begin with the string "va"


You can exclude certain variables from your varlist (like your ID vars) by:
 _all_ not(hhid personid)
NOTE:  There can be no space between the word "not" and the left parenthesis!

YES:  not( varlist )

NO:  not ( varlist )

ALSO:  The order must be varlist first, then not(varlist).

(3rd)

constant

String you want to append to the variable name as a prefix or suffix to make it a new name.



(4th)

Options
Explanation
before / after
Position of constant is to be before or after variable name.  [If no placement is specified then the constant will go before the existing variable name.]

N / C
If you have a variable list of both numeric and character type variables but only want to rename the character variables, use C.  Similarly if you only want to rename the numeric variables in the varlist, use N.  [If no vartype is specified then all variable types are renamed].  It can be upper or lower case N/n (numeric) or C/c (character).

SAScode

Prints the SAS code used by the macro to create the dataset with the renamed variables.  This could be useful if you want to modify the code in some way or if you want to simply see what was done.

 

How to use the REN_STEM macro:

Using the REN_STEM macro requires that you understand how to use the %include statement and that you know how to call a SAS macro.
 %include "LOCATION AND NAME OF A FILE THAT CONTAINS SAS CODE"; 
For example, if you have copied this file to "C:\SASmacros\", then you tell SAS about this macro by adding the following line to your SAS program:
 %include "C:\SASmacros\ren_stem.sas"; 
The include statement makes SAS aware of the REN_STEM macro which is in the file ren_stem.sas.  You call the macro by adding this to your code:
 %ren_stem(myData, myvar1 myvar2, pre_ , before); 

 

Examples


  ** Make SAS aware of the REN_STEM macro.**;
  %include "C:\SASmacros\ren_stem.sas"; 

  data new;
   set old;
  run;


  ** now call your macro by feeding whatever variables and whatever you want to tack
   *  on the beginning of the new variable names.  A comma separates what you feed
   *  the ren_stem macro.  **;
 *%ren_stem(dataset , list of variables , constant , options) **;
  %ren_stem(  new   , x1 x2 x3 x4 x5 y1 , round4_  , before  N);  ** eg. round4_x1 **;


  ** or : **;
  data out.new;
   set old;
  run;

  %ren_stem(out.new , x1 x2 x3 x4 x5 , _max , after sascode n);  ** eg. x1_max **;
 

  ** or all character variables : **;
  data new;
   set old;
  run;

  %ren_stem(myData , _numeric_ , _constant , after ); ** all numeric type variables eg. x1_constant **;


  ** or : **
  data new;
   set old;
  run;

  %ren_stem(testdata , x1--gender not(pid) , _min , after);   ** eg. x1_min gender_min **;

  proc contents data= new;
  run;

Wink Plone Theme by Quintagroup © 2013.

Personal tools
This is themeComment for Wink theme