Date: Mon, 27 Jul 2009 17:36:28 -0500
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 Loop
In-Reply-To: <5d13965e-bbd4-4d7e-b0c5-b29e89658dea@c1g2000yqi.googlegroups.com>
Content-Type: text/plain; charset=ISO-8859-1
What does the rest of your data look like? I'm not entirely sure how you're
doing this - is it always 6 back to 4 forward, or is it just all from some
point to now? Do you have a dataset with
spend001-spend460
or some other starting period well before 450? [Or, is 450 the start, but
you'll get plenty more periods as time goes on, and always start with 450?]
If so, you can do this fairly easily, if you don't mind using new variables
[which means you lose things like labels, or at least have to reapply them].
So:
data have;
format spend450-spend460 BEST12.;
array spends spend450-spend460;
do _n_ = 1 to 11;
spends[_n_]=_n_;
end;
run;
%let today=456; *the month that needs to be 0;
%let max = 460; *the largest month - you could also allow it to go
indefinitely, but that would waste some space. You could easily determine
this programmatically as well.;
data want;
set have;
array spends spend:;
array pms weekpm1-weekpm%eval(&today-450);
array ms weekm0-weekm%eval(&max-&today);
do _n_ = 1 to dim(Spends);
if _n_ + 449 < &today then do;
pms[dim(pms)+1-_n_]=spends[_n_];
end;
else do;
ms[_n_+449-&today+1]=spends[_n_];
end;
end;
drop spend:;
run;
Then just drop the spends and you have your fresh new data. This won't
retain variable labels, though, so if you have important variable labels
that need to be identical to the spends[ ] labels, you will want to do
another solution. (In general, I don't really suggest that this IS the best
solution - a proc datasets solution would be much better, I think - but
perhaps easier to understand than that).
-Joe
On Mon, Jul 27, 2009 at 3:21 PM, Hypnotik <Chris.Carruthers@gmail.com>wrote:
> Hi all,
>
> I am trying to do a rename loop of a set of variables. At present the
> code is like this:
>
> rename spend450 = weekpm6;
> rename spend451 = weekpm5;
> rename spend452 = weekpm4;
> rename spend453 = weekpm3;
> rename spend454 = weekpm2;
> rename spend455 = weekpm1;
> rename spend456 = weekm0;
> rename spend457 = weekm1;
> rename spend458 = weekm2;
> rename spend459 = weekm3;
> rename spend460 = weekm4;
>
> and so on, however I need the code to be more automated. The spend###
> is dependant on the week of the month so will continue until the end
> of the year. This is only required so that the rename does not have to
> be changed constantly and could possibly just do one change and the
> rest will fall into place.
>
> I have seen code such as:
>
> data test_out;
> set test_in;
> where Date between "01jan2002"d and "30jun2002"d;
> rename Price1-Price17 = UnitPriceProduct01-UnitPriceProduct17;
> run;
>
> The problem is I do not have a date between all I have is
> creation_date for the sake of argument could be "01jan2002"d.
>
> I think I need some sort of do loop, can you help me?
>
> Regards
>
> Hypno
>
|