LISTSERV at the University of Georgia
Menubar Imagemap
Home Browse Manage Request Manuals Register
Previous (more recent) messageNext (less recent) messagePrevious (more recent) in topicNext (less recent) in topicPrevious (more recent) by same authorNext (less recent) by same authorPrevious page (July 2002, week 5)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:   Wed, 31 Jul 2002 14:32: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:   a small question of array ?
Content-Type:   text/plain; charset=ISO-8859-1

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;


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