LISTSERV at the University of Georgia
Menubar Imagemap
Home Browse Manage Request Manuals Register
Previous messageNext messagePrevious in topicNext in topicPrevious by same authorNext by same authorPrevious page (August 2006, week 2)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:         Fri, 11 Aug 2006 04:25:20 -0700
Reply-To:     Frank Poppe <Frank.Poppe@PWCONS.COM>
Sender:       "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:         Frank Poppe <Frank.Poppe@PWCONS.COM>
Organization: http://groups.google.com
Subject:      Re: Renaming variables via a macro - Please Help
Comments: To: sas-l@uga.edu
In-Reply-To:  <1155226843.035081.51550@h48g2000cwc.googlegroups.com>
Content-Type: text/plain; charset="iso-8859-1"

There are approaches which seem simpler, but may be there are other reasons to do it this way. Getting the code to work I found the following issues:

1. The variable _N_ is automatically incremented at the start of each data step iteration. If you need a counter for which you yourself determine when to increment, use another variable. I have used N.

2. The IF LAST statement should be moved outside the IF name NE&idvarname block (in case your last variable is the ID var).

3. I'm not sure what you had in mind with the &new and &endchar macrovariable. There is no need to store the new variable names in macro variables, but because the new name is always the old one with the suffix appended. Anyway, you don't create a macro var &new&i, although you do try to use it. You only have to set the number of variable names (i.e. the current value of N) in the IF LAST statement. I have done so with the macro variable num.

4. When renaming you don't have to check on IDvarname, because that one will not be in the list of &old&i macrovariables (you skipped that one while creating the macro variables).

5. You cannot use a comment-statement (i.e. the syntax * comment ;) inside a statement. That's what you do when you generate the RENAME statement. Either use the /* comment */ form or use macro syntax like %* comment ; The macro syntax will make it only a comment inside the macro, and it will not be generated in the statement.

I ended up with the following code.

Data random; input Variable & $23. MonthEnd & $20. flag & $1.; cards; Totala Blanka 1 Totalb Blankb 1 Totalc Blankc 1 ; run;

%macro process(dsnrenv=,endchar=,idvarnam=); proc contents data=random noprint out=_vnames (keep=name); run; %let New=0;

data _null_; set _vnames end=last; put _ALL_ ; if (upcase(name) ne upcase("&idvarnam")) then do; n+1 ; call symput('old'!!compress(put(n,4.)),compress(name)); end; * Remember number of names, for looping; if last then call symput('num', put (n,4.) ); run;

%put num: &num ;

proc datasets nolist; modify &dsnrenv; rename %if &Num.=0 %then %put NOTE: File is empty; %else %do; %* Process the incremental files ; %do i = 1 %to &Num.; &&old&i=&&old&i.._&endchar %end; %end;; run; %mend process;

options mprint ; %process (dsnrenv=random,endchar=_new,idvarnam=Variable);

Good luck,

Frank Poppe PW Consulting the Netherlands

jeli0703@hotmail.co.uk schreef:

> Hi > > I have a data set and I am trying to rename a number of the variables > in it (this is an example but I am trying to rename monthend to > monthend_new and flag to flag_new) > > I have the following bit of code but am really struggling to sort it > please help!!!! > > > Data random; > input Variable & $23. MonthEnd & $20. flag & $1.; > cards; > Totala Blanka 1 > Totalb Blankb 1 > Totalc Blankc 1 > ; > run; > > %macro process(dsnrenv=,endchar=,idvarnam=); > proc contents data=random noprint out=_vnames (keep=name); > run; > %let New=0; > > data _null_; > set _vnames end=last; > if (upcase(name) ne upcase("&idvarnam")) then do; > _n_=_n_+1; > call symput('old'!!compress(put(_n_,4.)),compress(name)); > suff_var=left(compress(name||"&endchar")); > * Also capture the number of filenames, for looping; > if last then call symput(trim('New'||left(put(_n_,4.))),suff_var); > end; > run; > > proc datasets nolist; > modify &dsnrenv; > rename > %if &New.=0 %then %put NOTE: File is empty; > %else %do; > * Process the incremental files; > %do i = 1 %to &New.; > %if (%upcase("&&old&i") ne %upcase("&idvarnam")) %then %do; > &&old&i=&&&new&i > %end; > %end; > %end;; > run; %mend process; > %process (dsnrenv=random,endchar=_new,idvarnam=Variable); > > regards > J


Back to: Top of message | Previous page | Main SAS-L page