Date: Tue, 27 Jul 2004 09:47:07 +0200
Reply-To: "Groeneveld, Jim" <jim.groeneveld@VITATRON.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: "Groeneveld, Jim" <jim.groeneveld@VITATRON.COM>
Subject: Re: bulk rename of variables; was: RE: Macro Help
Content-Type: text/plain; charset="iso-8859-1"
Dear Kitty,
First of all you subject line may be more informative, e.g. "bulk rename of variables".
Secondly, shouldn't you apply four digit year indications? Below I have worked out your
question; the code yields four digit years in new names, but can easily be changed, as
indicated in there, to support two digit years.
======================================== cut here ========================================
%MACRO NewNames (RefMonth=, RefYear=);
%LOCAL Months M R Y;
%LET Months = Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec;
%* Determine reference month;
%DO R = 1 %TO 12;
%IF (%UPCASE(&RefMonth) EQ %UPCASE(%SCAN(&Months, &R, %STR( )))) %THEN
%GOTO StopLoop;
%END;
%StopLoop:
%* Generate renaming code;
%LET Y = &RefYear;
%IF (%LENGTH(&Y) EQ 2) %THEN %LET Y = 20&Y; %* four digit year;
%DO M = 1 %TO 48;
%IF (&R EQ 0) %THEN
%DO;
%LET R = 12;
%LET Y = %EVAL (&Y - 1); %* assumed never to dive below 2000;
%END;
%* now the actual renaming code, without semicolon;
Month&M %STR(=) %SCAN(&Months, &R, %STR( ))&Y /* with four digit year */
%* If you explicitly want a two digit year, change this into;
/* Month&M %STR(=) %SCAN(&Months, &R, %STR( ))%SUBSTR(&Y,3) */ /* with two digit year */
%LET R = %EVAL (&R - 1);
%END;
%MEND NewNames;
* test macro result in comment (semicolon ends %PUT);
%PUT %NewNames (RefMonth=jun, RefYear=04);
* test macro result in comment (semicolon ends %PUT);
%PUT %NewNames (RefMonth=jun, RefYear=03);
* test macro result in comment (semicolon ends %PUT);
%PUT %NewNames (RefMonth=jun, RefYear=2000);
* test macro result in comment (semicolon ends %PUT);
%PUT %NewNames (RefMonth=jun, RefYear=1998);
* test macro result in comment (semicolon ends %PUT);
%PUT %NewNames (RefMonth=dec, RefYear=04);
* test macro result in comment (semicolon ends %PUT);
%PUT %NewNames (RefMonth=jan, RefYear=04);
/* Apply like in:
DATA Process;
SET History (RENAME=(%NewNames(RefMonth=jun, RefYear=04)));
.........;
RUN;
*/
======================================== cut here ========================================
Regards - Jim.
--
. . . . . . . . . . . . . . . .
Jim Groeneveld, MSc.
Biostatistician
Science Team
Vitatron B.V.
Meander 1051
6825 MJ Arnhem
Tel: +31/0 26 376 7365
Fax: +31/0 26 376 7305
Jim.Groeneveld@Vitatron.com
www.vitatron.com
My computer says 'beep'. What's wrong with that? It's talking too much!
[common disclaimer]
-----Original Message-----
From: Kitty Lee [mailto:kitty_lee04@YAHOO.COM]
Sent: Monday, July 26, 2004 20:47
To: SAS-L@LISTSERV.UGA.EDU
Subject: Macro Help
Hi,
I am trying to learn how to write a macro program. Since I just started to learn writing macros, I need your help. Here's my task:
I already wrote a program to find out what month of data that I use. For example, the current month is July 2004. However, I sometime may use the historical data, which is June 2004 in this example, to do analysis. Now, I have a global marco variable called yyymmmm. The database we have only contains generic variables such as month1, month2, month3 ...month48. It was set up that way because it's easier for the statisticians to do the time series analysis. However, I would like to create an output file that has the name in month and year instead of month1, month2...For example, month1 = jul04, month2 = jun04, month3= may04, etc....It was too confusing to use the prefix "month" because the users of the reports didn't know which dataset that the statisticians had used to generate the reports if the statisticians forgot to make a note in the report.
So, I was wondering if I could setup a macro that could do a rename for me, I don't have to change the program every time I switch to different database. I think the most difficult part of this task is that x1 is month04 in this month but if I use the historical data, June 04 data, month1 is jun04, month2 is may04 etc...
Any ideas will be greatly appreciated.
Thanks so much for your help in advance!!
kitty
---------------------------------
Do you Yahoo!?
Yahoo! Mail - 50x more storage than other providers!
|