Date: Fri, 23 Apr 2010 11:07:18 -0400
Reply-To: Joe Whitehurst <joewhitehurst@GMAIL.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Joe Whitehurst <joewhitehurst@GMAIL.COM>
Subject: Re: Concatenating variable names using synchronized arrays (have
and need data included)
In-Reply-To: <980522.7378.qm@web57006.mail.re3.yahoo.com>
Content-Type: text/plain; charset=ISO-8859-1
Paul,
Since you have SAS/AF here, with a slight modification, is another
alternative way to do what you want:
length varnames msg varname $200 memname $32 tabledata $41 libname $8;
init:
libname='FINALSAS';
dsid_tableNames=open('sashelp.vtable(where=(libname='||quote(libname)||'))','i');
call set(dsid_tableNames);
dsid_names=open('sascat1.ba_varnames','I');
call set(dsid_names);
do while(fetch(dsid_tableNames)=0);
memname=compress(memname);
tabledata='finalsas.'||memname;
link rename;
end;
rcclose=close(dsid_tablenames);
if dsid_names then rcclose=close(dsid_names);
return;
rename:
i=0;
dsid_data=open(tabledata,'v');
do while(fetch(dsid_names)=0);
i+1;
varnames=compress(varnames);
varname=varname(dsid_data,i+1);
if upcase(substr(varname,1,3))='COL' & varname^=varnames then
rc=modvar(dsid_data,varname,varnames,varname);
msg=sysmsg();
if msg^=' ' then put msg= varname= varnames=;
end;
rc=rewind(dsid_names);
if dsid_data then rcclose=close(dsid_data);
return;
Joe
On Fri, Apr 23, 2010 at 10:36 AM, Paul Miller <pjmiller_57@yahoo.com> wrote:
> Hi Mike,
>
> Your solution is just great. I'm always amazed by how good programmers can
> take something that looks difficult and make it seem easy.
>
> I'm not sure if this will be the way to go with my final code, because I
> may be working with as many as 15-20 variables. Still, I think I just
> learned something and that always makes me happy.
>
> Thanks,
>
> Paul
>
> --- On Thu, 4/22/10, Mike Zdeb <msz03@albany.edu> wrote:
>
>
> From: Mike Zdeb <msz03@albany.edu>
> Subject: Re: [SAS-L] Concatenating variable names using synchronized arrays
> (have and need data included)
> To: SAS-L@LISTSERV.UGA.EDU
> Received: Thursday, April 22, 2010, 1:30 PM
>
>
> hi ... some "no array" possibilities ...
>
> data have;
> input subject d c p;
> cards;
> 1 1 1 1
> 2 1 1 0
> 3 1 0 0
> 4 1 0 0
> 5 0 1 0
> 6 0 0 1
> 7 0 0 0
> ;
> run;
>
> * mork work here;
> proc format;
> value _d 1 = 'doxorubicin' 0 = ' ';
> value _c 1 = 'cyclophosphamide' 0 = ' ';
> value _p 1 = 'paclitaxel' 0 = ' ';
> run;
>
> * less work here;
> data want;
> length testvar $50;
> set have;
> testvar = catx(',',put(d,_d.),put(c,_c.),put(p,_p.));
> run;
>
> * if you don't mind notes in the LOG re SCAN with a bad argument;
>
> data want;
> retain stuff "doxorubicin,cyclophosphamide,paclitaxel";
> length testvar $50;
> set have;
> testvar = catx(',',scan(stuff,(d eq 1)*1),
> scan(stuff,(c eq 1)*2),
> scan(stuff,(p eq 1)*3));
> run;
>
>
> each gives you ...
>
> subject d c p testvar
> 1 1 1 1 doxorubicin,cyclophosphamide,paclitaxel
> 2 1 1 0 doxorubicin,cyclophosphamide
> 3 1 0 0 doxorubicin
> 4 1 0 0 doxorubicin
> 5 0 1 0 cyclophosphamide
> 6 0 0 1 paclitaxel
> 7 0 0 0
>
>
>
>
> --
> Mike Zdeb
> U@Albany School of Public Health
> One University Place (Room 119)
> Rensselaer, New York 12144-3456
> P/518-402-6479 F/630-604-1475
>
> > Hello Everyone,
> >
> > I want to concatenate some variable names using synchronized arrays. I've
> pasted an example of the kind of data I have and need as well as a piece
> > of code that I thought would work.
> >
> > What am I doing wrong here?
> >
> > Thanks,
> >
> > Paul
> >
> > data have;
> > input subject doxorubicin $ cyclophosphamide $ paclitaxel $;
> > cards;
> > 1 1 1 1
> > 2 1 1 0
> > 3 1 0 0
> > ;
> > run;
> >
> > data need;
> > input subject doxorubicin $ cyclophosphamide $ paclitaxel $ testvar $50.;
> > cards;
> > 1 1 1 1 doxorubicin,cyclophosphamide,paclitaxel
> > 2 1 1 0 doxorubicin,cyclophosphamide
> > 3 1 0 0 doxorubicin
> > ;
> > run;
> >
> > data need;
> > set have;
> > array values(*) doxorubicin cyclophosphamide paclitaxel;
> > array names(*) $25. names1-names3 ("doxorubicin" "cyclophosphamide"
> "paclitaxel");
> > format testvar $250.;
> > do i = 1 to dim(values);
> > if values(i) > 0 and testvar eq '' then testvar = names(i);
> > else if values(i) > 0 and testvar ne '' then testvar = testvar || ', ' ||
> names(i);
> > end;
> > drop names1-names3 i;
> > run;
> >
> >
> >
> >
> >
> >
>
>
>
>
>
|