Date: Sat, 1 Oct 2005 13:02:29 +0000
Reply-To: toby dunn <tobydunn@HOTMAIL.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: toby dunn <tobydunn@HOTMAIL.COM>
Subject: Re: Help: macro to rename variable in a set statement not working
In-Reply-To: <d05681f44a2aa36a70371ce87a3ffa76.43464@mygate.mailgate.org>
Content-Type: text/plain; format=flowed
Steve,
I only have version 8.2 Le at home so I would count the number of variables
in th evarlist parameter a little different in v9 and above but this does
work.
%macro rename(varlist= , postfix=) ;
%local vars listcnt I ;
%let vars = ;
%let listcnt = %eval( (%length(&varlist) -
%length(%sysfunc(compress(&varlist))) ) + 1) ;
%put &listcnt;
%do I = 1 %to &listcnt ;
%let vars = &vars %scan(&varlist,&i,' ') = %scan(&varlist,&i,' ')&postfix
;
%end ;
&vars
%mend rename ;
data s1;
x1 = 1; y1 = 3;
run;
data s2;
x1 = 5; y1 = 6;
run;
data s3;
x1 = 9; y1 = 16;
run;
data three ;
set s1 (rename = (%rename(varlist=x1 y1 , postfix=_1))) ;
set s2 (rename = (%rename(varlist=x1 y1 , postfix=_2))) ;
set s3 (rename = (%rename(varlist=x1 y1 , postfix=_3))) ;
run ;
proc print
data = three ;
run ;
Toby Dunn
From: Steve Jones <st_jones77@YAHOO.COM>
Reply-To: Steve Jones <st_jones77@YAHOO.COM>
To: SAS-L@LISTSERV.UGA.EDU
Subject: Help: macro to rename variable in a set statement not working
Date: Sat, 1 Oct 2005 12:36:00 +0000
Hello all,
I have datasets (3 here) with same variable names. I need to
merge them and rename the variables with a postfix corresponding
to the dataset they came from. For example, if variable x1 is from
the first dataset, I want it to be x1_1:
data all;
merge s1(rename=(x1 = x1_1 y1 = y1_1))
s2(rename=(x1 = x1_2 y1 = y1_2))
s3(rename=(x1 = x1_3 y1 = y1_3));
run;
I have written a macro that doesn't work in the renaming section.
The sample datasets and macros are below..
Thank you in advance for your help in resolving the issue.
SJ.
*** The datasets ***;
data s1;
x1 = 1; y1 = 3;
run;
data s2;
x1 = 5; y1 = 6;
run;
data s3;
x1 = 9; y1 = 16;
run;
*** The macros ***;
%let sdata = s1 s2 s3;
%let svar = x1 y1;
options macrogen symbolgen;
%macro merged(dataset, xvar);
%let i =1;
%do %while (%scan(&sdata, &i) ne );
%let segm&i = %scan(&sdata, &i);
%let i = %eval(&i+1);
%end;
%let totsegs = %eval(&i-1);
%let j =1;
%do %while (%scan(&svar, &j) ne );
%let var&j = %scan(&svar, &j);
%let j = %eval(&j+1);
%end;
%let totvars = %eval(&j-1);
data all;
merge %do i = 1 %to &totsegs;
&&segm&i (rename=(%do j = 1 %to &totvars;
&&var&j = &&var.&j._&i
%end;))
%end;;
run;
%mend merged;
%merged(&sdata, &svar);
--
Posted via Mailgate.ORG Server - http://www.Mailgate.ORG