Date: Thu, 1 Aug 2002 06:28:29 -0700
Reply-To: amy <amywang11790@YAHOO.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: amy <amywang11790@YAHOO.COM>
Organization: http://groups.google.com/
Subject: Re: a small question of array ?
Content-Type: text/plain; charset=ISO-8859-1
Thank you, Roger, it works well and much efficient!
Roger Lustig <trovato@bellatlantic.net> wrote in message news:<3D485CB1.2080207@bellatlantic.net>...
> Amy:
> You seem to be on the right track for defining the arrays, but there are
> much easier ways of doing what you want to do!
>
> First, there's a NOBS value defined for any non-sequential (i.e., tape)
> SAS data set. So your data _null_ step would read like this:
>
> data _null_;
> set &p_infile nobs=num_obs;
> call symput('N_OBS',put(num_obs,12.));
> run;
>
> Also, to accumulate the values of m_tot, c_tot, etc., you either need to
> add a RETAIN statement and an initialization or use the form
>
> m_tot+ma{i};
>
> (no equals sign!).
>
> But you don't really care about how many records there are. What you
> want is simply some summary statistics.
>
> For instance:
>
> proc summary data=test;
> var m c ;
> output out=summ sum(m)=m_tot sum(c)=c_tot ;
> run;
> data _null_;
> set summ;
> do until (eof);
> set test end=eof;
> w_tot +
> (m/m_tot) / (c/c_tot) * cca;
> end;
> w_rate=max(0,w_tot/c_tot);
> call symput('w_rate',put(w_rate,best12.));
> run;
>
> This can also be done as an SQL query, but I'll leave that to someone else.
>
> Best,
>
> Roger
>
>
> amy wrote:
>
> > How to define a array with variable array-elements?
> >
> > Hi, all,
> > --I intended to do following caculation. (Attached are the codes, you
> > may skip some part,I put all code here,just for the sake of less
> > chance of misunderstanding)
> >
> > --The number of record is changing according to different data set, so
> > I intend to a array like: array ma{&num_rec} 8. m1-m&num_rec;
> > This does not work.
> >
> > Any insight how to do this? TIA. Amy
> >
> > data test;
> > input m c cc;
> > datalines;
> > 40 0 0
> > 30 0 0
> > 20 0 0
> > 10 0 0
> > ;
> > run;
> >
> > %macro get_weight(p_infile=,col1=,col2=,col3=,p_w_rate=w_rate);
> > /*try to get the number of records in the data set*/
> > data _null_;
> > set &p_infile;
> > retain n 0;
> > n=n+1;
> > call symput('num_rec',n);
> > run;
> >
> > proc transpose data=&p_infile(keep=&col1) out=out1 PREFIX=m ;
> > proc transpose data=&p_infile(keep=&col2) out=out2 PREFIX=c;
> > proc transpose data=&p_infile(keep=&col3) out=out3 PREFIX=cc;
> >
> > data finalset;
> > merge out1 out2 out3;
> > run;
> >
> > data output1;
> > set finalset;
> > /* want to change this m1-m4 to some thing like m1-m&num_rec*/
> > array ma{&num_rec} 8. m1-m4;
> > array ca{&num_rec} 8. c1-c4;
> > array cca{&num_rec} 8. cc1-cc4;
> > array m_perc{&num_rec} 8.;
> > array c_perc{&num_rec} 8.;
> > array w_perc{&num_rec} 8.;
> >
> > &p_w_rate=0;
> > m_tot=0;
> > c_tot=0;
> > w_tot=0;
> >
> > /*do some caculate*/
> > do i=1 to &num_rec;
> > m_tot=m_tot+ma{i};
> > c_tot=c_tot+ca{i};
> > end;
> >
> > do i=1 to &num_rec;
> > m_perc{i}=ma{i}/m_tot;
> > c_perc{i}=ca{i}/c_tot;
> > end;
> >
> > do i=1 to &num_rec;
> > w_perc{i}=m_perc{i}/c_perc{i}*cca{i};
> > w_tot=w_tot+w_perc{i};
> > end;
> > if w_tot=0 or c_tot=0 then &p_w_rate=0; else &p_w_rate=w_tot/c_tot;
> > keep &p_w_rate;
> > %mend;
> >
> > %get_weight(p_infile=test,col1=m,col2=c,col3=cc,p_w_rate=w_rate);
> > data _null_;
> > set output1;
> > call symput('w_rate',w_rate);
> > run;
> > %put &w_rate;
> > run;
> >
|