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 (December 2009, week 3)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:         Tue, 15 Dec 2009 09:55:28 -0800
Reply-To:     Dale McLerran <stringplayer_2@YAHOO.COM>
Sender:       "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:         Dale McLerran <stringplayer_2@YAHOO.COM>
Subject:      Re: SAS dynamic arrays
In-Reply-To:  <20091215113231.13934sx3ab1e0q0v@webmail.nfit.au.dk>
Content-Type: text/plain; charset=iso-8859-1

--- On Tue, 12/15/09, Håkan Lane <hlane@CLS.DK> wrote:

> From: Håkan Lane <hlane@CLS.DK> > Subject: Re: SAS dynamic arrays > To: SAS-L@LISTSERV.UGA.EDU > Date: Tuesday, December 15, 2009, 2:32 AM > Thank you all for providing nice > help. It works out a lot better now > with another change, but a problem is that sampled only is > set the > last time that the person appears, captured by last.pnr. I > would have > liked sampled to be set every time and then a record > written only the > last time. > > For example, current records of > > pnr=1,aar=2001 > pnr=1,aar=2002 > pnr=1,aar=2003 > > should result in > pnr=1,sampled2001=1,sampled2002=1,sampled2003=1 > and not as now only in pnr=1,sampled2003=1. > > How do I do that? > > > DATA libout.pdata; > SET cpr; > byear=aar-alder; > %let numyears=%eval (&slutaar-&startaar+1); > Array sampled{&numyears} sampled&startaar - > sampled&slutaar; > %let index=aar-&startaar+1; > sampled{&index}=1; > DROP alder numyears; > IF last.pnr THEN output; > ELSE DO; > END; > RUN; > > Regards, > > Hkan Lane > > > > > > > -----Original Message----- > > From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] > On Behalf Of > > Håkan Lane > > Sent: Monday, December 14, 2009 7:31 AM > > To: SAS-L@LISTSERV.UGA.EDU > > Subject: SAS dynamic arrays > > > > I would like to implement an array the size of which is not known > > a priori. Instead of declaring an implicit value for the number of > > elements, I wish to find this value from a variable. This can be > > done in other languages including Fortran. > > > > My attempt to code this as shown below fails. > > > > DATA libout.pdata; > > SET cpr; > > byear=aar-alder; > > numyears=&slutaar-&startaar+1; > > Array sampled{numyears} sampled&startaar - sampled&slutaar; > > sampled{aar}=1; > > DROP alder numyears; > > IF last.pnr THEN output; > > ELSE DO; > > END; > > RUN; > > > > The idea is to replace a structure with one record every year that > > a person was in our records with their age so an array with a 1 if > > they were in and 0 if they weren't. The age (alder from above) is > > represented with a birthyear. This would quite obviously save a lot > > of redundant data. The issue is that we might run the program with > > different start and end years. That is why it would be better for > > them to be external constants. > > > > Thanks in advance. > > > > Regards, > > > > Hkan Lane > > >

Hkan,

Use a RETAIN statement to hold the values of sampled&startaar through sampled&slutaar over until the last record. You will need to initialize retained variables to zero or missing with the first record for every pnr value. Thus, the following code should work:

DATA libout.pdata; retain sampled&startaar - sampled&slutaar; SET cpr; by pnr; byear=aar-alder; numyears=&slutaar-&startaar+1; Array sampled{*} sampled&startaar - sampled&slutaar; if first.pnr then do i=1 to numyears; sampled{i}=0; end; sampled{aar}=1; DROP alder numyears i; IF last.pnr THEN output; RUN;

Dale

--------------------------------------- Dale McLerran Fred Hutchinson Cancer Research Center mailto: dmclerra@NO_SPAMfhcrc.org Ph: (206) 667-2926 Fax: (206) 667-5977 ---------------------------------------


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