| Date: | Mon, 14 Dec 2009 09:04:20 -0500 |
| Reply-To: | Mike Rhoads <RHOADSM1@WESTAT.COM> |
| Sender: | "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU> |
| From: | Mike Rhoads <RHOADSM1@WESTAT.COM> |
| Subject: | Re: SAS dynamic arrays |
|
| In-Reply-To: | <20091214133122.10436wcl9ieigpp6@webmail.nfit.au.dk> |
| Content-Type: | text/plain; charset="iso-8859-1" |
If you are going to calculate the number of years needed, it needs to be done with a macro variable in order to use it for dimensioning the array:
%let numyears=%eval(&slutaar-&startaar+1);
Array sampled{&numyears} sampled&startaar - sampled&slutaar;
sampled{aar}=1;
DROP alder;
By the way, I always like to put such macro statements outside of (prior to) DATA and PROC steps. This reduces the confusion between macro statements and other statements that need to execute within the context of a SAS step.
Better yet, use the * to dimension your array, and you don't have to worry about calculating the number of years required at all:
Array sampled{*} sampled&startaar - sampled&slutaar;
Mike Rhoads
RhoadsM1@Westat.com
-----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,
Håkan Lane
|