Date: Fri, 19 Dec 2008 09:29:34 -0600
Reply-To: Joe Matise <snoopy369@GMAIL.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Joe Matise <snoopy369@GMAIL.COM>
Subject: Re: Rename variable names into yyyymmm_[variablename] format
In-Reply-To: <2089d922-3234-49f9-9f76-f52d84e7eea8@e25g2000vbe.googlegroups.com>
Content-Type: text/plain; charset=ISO-8859-1
You're going to have to reconsider at least one element; the variable name
cannot start with a number. But you can either do sales_2008jan or
something similar.
In general, renaming variables programmatically is not difficult. For
example:
%let month = jan;
%let year = 2008;
data have;
input x y z a b c;
cards;
1 2 3 4 5 6
2 3 4 5 6 7
4 5 6 7 8 9
;;;;
run;
proc contents data=have out=conts(keep=name);
run;
proc sql noprint;
select 'rename '||compress(name)||' = '||compress(name)||"_&year.&month.;"
into :renamelist separated by " " from conts;
quit;
data want;
set have;
&renamelist;
run;
assuming you don't have too many variables such that you'd hit the command
length limit; if you did you would do it through an include file or similar.
-Joe
On Fri, Dec 19, 2008 at 9:15 AM, sasbeginner <pattukuttani@gmail.com> wrote:
> Hi,
>
> I need some help in coding a macro to rename all my existing varibales
> into something with the date + varibale name format.
>
> The code that I currently have is given below. The variable name array
> cannot be changed. So am thinking of writing a macro which would take
> in the names of the variables from the array from the following code
> and then change it to something like 2008jan_sales etc for each of my
> variables.
>
> Please let me know if I can do this through macro if yes please show
> me a guide line.
>
> I sincerely thanks each one of you here for having look at my post and
> I appreicate the time and effort.
>
> Thanks again and Merry christmas to all !!!!
>
> libname data '';
> data curdata ;
> infile "";
> informat sales_id $9.;
> informat prd_code $6.;
> informat upc_code $7.;
> informat yrmon $6.;
> informat salescount 5.;
> informat salesdollars 5.;
> informat newsales 5.;
> informat totalsales 5.;
> informat totalsalesdol 5.;
> informat totalnewsales 5.;
> format sales_id $9.;
> format prd_code $6.;
> format upc_code $7.;
> format yrmon $6.;
> format salescount 5.;
> format salesdollars 5.;
> format newsales 5.;
> format totalsales 5.;
> format totalsalesdol 5.;
> format totalnewsales 5.;
>
> input sales_id $ prd_code $ upc_code $ yrmon$ salescount salesdollars
> newsales totalsales totalsalesdol totalnewsales ;
> format dat yymmn6.;
> mon = substr(yrmon,1,2);
> yr = substr(yrmon,3,4);
> dat = mdy(mon, 1, yr);
> run;
> proc print data=curdata;
> run;
>
> proc sort data=curdata out=srtdata nodupkey ;
> by descending dat;
> run;
>
>
>
> data srtdata;
> set srtdata end=eof;
> retain fmtname "$mon";
> i+1;
> start = yrmon;
> label=i;
> if eof then call symput("max",i);
> run;
>
>
> proc format cntlin = srtdata fmtlib;
> run;
>
> proc sort data=curdata;
> by sales_id prd_code upc_code;
> run;
>
> data move(drop=yrmon num i dat yr mon salescount salesdollars newsales
> totalsales totalsalesdol totalnewsales) ;
> set curdata;
> by sales_id prd_code upc_code ;
> retain salescounts1-salescounts%sysfunc(compress(&max.));
> retain salesdollarss1-salesdollarss%sysfunc(compress(&max.));
> retain newsaless1-newsaless%sysfunc(compress(&max.));
> retain totalsaless1-totalsaless%sysfunc(compress(&max.));
> retain totalsalesdols1-totalsalesdols%sysfunc(compress(&max.));
> retain totalnewsaless1-totalnewsaless%sysfunc(compress(&max.));
> num=put(yrmon,$mon.);
> put num=;
> array salescounts(*) salescounts1-salescounts%sysfunc(compress
> (&max.));
> array salesdollarss(*) salesdollarss1-salesdollarss%sysfunc(compress
> (&max.));
> array newsaless(*) newsaless1-newsaless%sysfunc(compress(&max.));
> array totalsaless(*) totalsaless1-totalsaless%sysfunc(compress
> (&max.));
> array totalsalesdols(*) totalsalesdols1-totalsalesdols%sysfunc(compress
> (&max.));
> array totalnewsaless(*) totalnewsaless1-totalnewsaless%sysfunc(compress
> (&max.));
>
> if first.upc_code or first.prd_code then do ;
>
> do i =1 to &max. ;
>
> salescounts(i) = 0;
> salesdollarss(I) = 0;
> newsaless(I) = 0;
> totalsaless(I) = 0;
> totalsalesdols(I) = 0;
> totalnewsaless(I)=0;
>
> end;
> end;
> salescounts(num) = salescount;
> salesdollarss(num) = salesdollars;
> newsaless(num) = newsales;
> totalsaless(num) = totalsales;
> totalsalesdols(num) = totalsalesdol;
> totalnewsaless(num) = totalnewsales;
> totalsaless(num) = totalsales;
>
> if last.upc_code or last.prd_code then output;
>
>
> run;
>
> data newds;
> set move;
> retain salescounts1-salescounts22;
> array salescounts(*) salescounts1-salescounts22;
> do i = 22 to 2;
> salescounts(i) = salescounts(i-1);
> salescounts(2) = salescounts(1);
> salescounts(1) = 0;
> end;
>