![]() |
Don't Be a SAS® Dinosaur:
Modernize Your SAS Programs
Variable Names: Rename a Numbered Range List |
You have a set of variables whose names start with the same "root" characters and end with a number in sequence. You wish to change the "root" part of each name and/or the numeric "suffix" values.
The traditional rename syntax has each individual variable listed with its corresponding new name.
|
The original approach is to build a rename list using a macro loop. Download this program
%macro renameloop(oldprefix=X,newprefix=Y,
oldstart=1,newstart=1,count=1);
%local i;
%do i=1 %to &count;
&oldprefix%eval(&oldstart+&i-1) = &newprefix%eval(&newstart+&i-1)
%end;
%mend renameloop;
proc sort data=sashelp.pricedata out=pricedata nodupkey;
by Date;
run;
%let renames=%renameloop(oldprefix=Price,newprefix=UnitPriceProduct,count=17);
options symbolgen;
data Prices2002;
set pricedata;
where Date between "01jan2002"d and "30jun2002"d;
rename &renames ;
run;
options nosymbolgen;
proc print data=Prices2002 heading=h;
id Date;
format Date date9.;
var UnitPrice:;
title1 "Product Prices throughout First Half of 2002";
run;
SAS Log
495 %macro renameloop(oldprefix=X,newprefix=Y,
496 oldstart=1,newstart=1,count=1);
497 %local i;
498 %do i=1 %to &count;
499 &oldprefix%eval(&oldstart+&i-1) = &newprefix%eval(&newstart+&i-1)
500 %end;
501 %mend renameloop;
502 proc sort data=sashelp.pricedata out=pricedata nodupkey;
503 by Date;
504 run;
NOTE: There were 1020 observations read from the data set SASHELP.PRICEDATA.
NOTE: 960 observations with duplicate key values were deleted.
NOTE: The data set WORK.PRICEDATA has 60 observations and 28 variables.
505 %let renames=%renameloop(oldprefix=Price,newprefix=UnitPriceProduct,count=17);
506 options symbolgen;
507 data Prices2002;
508 set pricedata;
509 where Date between "01jan2002"d and "30jun2002"d;
SYMBOLGEN: Macro variable RENAMES resolves to Price1 = UnitPriceProduct1 Price2 =
UnitPriceProduct2 Price3 = UnitPriceProduct3 Price4 = UnitPriceProduct4
Price5 = UnitPriceProduct5 Price6 = UnitPriceProduct6 Price7 =
UnitPriceProduct7 Price8 = UnitPriceProduct8 Price9 = UnitPriceProduct9
Price10 = UnitPriceProduct10 Price11 = UnitPriceProduct11 Price12 =
UnitPriceProduct12 Price13 = UnitPriceProduct13 Price14 =
UnitPriceProduct14 Price15 = UnitPriceProduct15 Price16 =
UnitPriceProduct16 Price17 = UnitPriceProduct17
510 rename &renames ;
511 run;
NOTE: There were 6 observations read from the data set WORK.PRICEDATA.
WHERE (Date>='01JAN2002'D and Date<='30JUN2002'D);
NOTE: The data set WORK.PRICES2002 has 6 observations and 28 variables.
512 options nosymbolgen;
513 proc print data=Prices2002 heading=h;
514 id Date;
515 format Date date9.;
516 var UnitPrice:;
517 title1 "Product Prices throughout First Half of 2002";
518 run;
NOTE: There were 6 observations read from the data set WORK.PRICES2002.
SAS Listing Output
Product Prices throughout First Half of 2002
Unit Unit Unit Unit Unit Unit Unit Unit Unit
Price Price Price Price Price Price Price Price Price
date Product1 Product2 Product3 Product4 Product5 Product6 Product7 Product8 Product9
01JAN2002 52.3 115.0 33.4 67.9 36.0 48.60 42 59 65.2
01FEB2002 52.3 115.0 33.4 67.9 36.0 48.60 42 59 65.2
01MAR2002 52.3 103.5 33.4 67.9 28.8 48.60 42 59 65.2
01APR2002 52.3 115.0 33.4 67.9 36.0 48.60 42 59 65.2
01MAY2002 52.3 115.0 33.4 67.9 36.0 48.60 42 59 65.2
01JUN2002 52.3 115.0 33.4 67.9 36.0 43.74 42 59 65.2
Unit Unit Unit Unit Unit Unit Unit Unit
Price Price Price Price Price Price Price Price
date Product10 Product11 Product12 Product13 Product14 Product15 Product16 Product17
01JAN2002 56.90 171.40 147.0 122.0 53 120.20 83 80.5
01FEB2002 51.21 171.40 147.0 122.0 53 120.20 83 80.5
01MAR2002 56.90 171.40 147.0 122.0 53 120.20 83 80.5
01APR2002 56.90 154.26 147.0 122.0 53 108.18 83 80.5
01MAY2002 56.90 171.40 132.3 122.0 53 120.20 83 80.5
01JUN2002 56.90 171.40 147.0 103.7 53 120.20 83 80.5
|
![]() |
|
An alternate approach is to use the numbered range list form of the RENAME= data set option and/or RENAME statement. Download this programproc sort data=sashelp.pricedata out=pricedata nodupkey; by Date; run; data Prices2002; set pricedata; where Date between "01jan2002"d and "30jun2002"d; rename Price1-Price17 = UnitPriceProduct01-UnitPriceProduct17; run; proc print data=Prices2002 heading=h; id Date; format Date date9.; var UnitPrice:; title1 "Product Prices throughout First Half of 2002"; run; SAS Log
530 proc sort data=sashelp.pricedata out=pricedata nodupkey;
531 by Date;
532 run;
NOTE: There were 1020 observations read from the data set SASHELP.PRICEDATA.
NOTE: 960 observations with duplicate key values were deleted.
NOTE: The data set WORK.PRICEDATA has 60 observations and 28 variables.
533 data Prices2002;
534 set pricedata;
535 where Date between "01jan2002"d and "30jun2002"d;
536 rename Price1-Price17 = UnitPriceProduct01-UnitPriceProduct17;
537 run;
NOTE: There were 6 observations read from the data set WORK.PRICEDATA.
WHERE (Date>='01JAN2002'D and Date<='30JUN2002'D);
NOTE: The data set WORK.PRICES2002 has 6 observations and 28 variables.
538 proc print data=Prices2002 heading=h;
539 id Date;
540 format Date date9.;
541 var UnitPrice:;
542 title1 "Product Prices throughout First Half of 2002";
543 run;
NOTE: There were 6 observations read from the data set WORK.PRICES2002.
SAS Listing Output
Product Prices throughout First Half of 2002
Unit Unit Unit Unit Unit Unit
Price Price Price Price Price Price
date Product01 Product02 Product03 Product04 Product05 Product06
01JAN2002 52.3 115.0 33.4 67.9 36.0 48.60
01FEB2002 52.3 115.0 33.4 67.9 36.0 48.60
01MAR2002 52.3 103.5 33.4 67.9 28.8 48.60
01APR2002 52.3 115.0 33.4 67.9 36.0 48.60
01MAY2002 52.3 115.0 33.4 67.9 36.0 48.60
01JUN2002 52.3 115.0 33.4 67.9 36.0 43.74
Unit Unit Unit Unit Unit Unit
Price Price Price Price Price Price
date Product07 Product08 Product09 Product10 Product11 Product12
01JAN2002 42 59 65.2 56.90 171.40 147.0
01FEB2002 42 59 65.2 51.21 171.40 147.0
01MAR2002 42 59 65.2 56.90 171.40 147.0
01APR2002 42 59 65.2 56.90 154.26 147.0
01MAY2002 42 59 65.2 56.90 171.40 132.3
01JUN2002 42 59 65.2 56.90 171.40 147.0
Unit Unit Unit Unit Unit
Price Price Price Price Price
date Product13 Product14 Product15 Product16 Product17
01JAN2002 122.0 53 120.20 83 80.5
01FEB2002 122.0 53 120.20 83 80.5
01MAR2002 122.0 53 120.20 83 80.5
01APR2002 122.0 53 108.18 83 80.5
01MAY2002 122.0 53 120.20 83 80.5
01JUN2002 103.7 53 120.20 83 80.5
Advantages
of the
alternate
approach:
|
|
Back to SAS Dinosaur home page Printable copy of this page (without sample output)
SAS and all other SAS Institute Inc. product or service names are registered trademarks
or trademarks of SAS Institute Inc. in the USA and other countries.
|