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 (November 2011, week 2)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:         Thu, 10 Nov 2011 23:41:16 -0500
Reply-To:     Paul Dorfman <sashole@BELLSOUTH.NET>
Sender:       "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:         Paul Dorfman <sashole@BELLSOUTH.NET>
Subject:      Re: arrays and nested loops
Comments: To: Mike Zdeb <mzdeb@ALBANY.EDU>
Content-Type: text/plain; charset=ISO-8859-1

Mike,

Agreed, and I would stretch it going on to say that perhaps sometimes it is better to have two arrays and a single loop than one array and two loops. The idea behind below is to have the elements in the second array shifted by 1 related to the first, hence only a single loop is needed, and the arrays can be made implicit, which, in my (rather lonely, albeit Ian-shared) opinion renders better code clarity than the explicit variant. (Many a sas- ler will note that the concept is not entirely foreign to creating a "lag" by merging a file with its own copy castrated with "firstobs=2" scalpel.)

data have ; input m1-m11 ; cards ; 20 20 50 50 2 30 5 100 100 40 40 run ;

data need ; set have ; array x m1- m10 ; array y m2- m11 ; array z $1 df1-df10 ;

do over x ; z = char ("NY", 1 + ((x min y) < 9)) ; end ; put (x[*])(4.-r) / (y[*])(4.-r) / (z[*])($4.-r) ; run ; ---------------------------------------- 20 20 50 50 2 30 5 100 100 40 20 50 50 2 30 5 100 100 40 40 N N N Y Y Y Y N N N

Kind regards ------------ Paul Dorfman Jax, FL ------------

On Thu, 10 Nov 2011 17:23:42 +0000, Zdeb, Michael S <mzdeb@ALBANY.EDU> wrote:

>hi ... I think that the two loops might make this more complicated than necessary ... data test1;set months;array m(11);array df(11) $1 (11*'N');do _n_ = 1 to 9; df(_n_) = ifc(m(_n_+1) lt 9 or m(_n_+2) lt 9 , 'Y', 'N');end;run; you could use the asterisks and dim function in the above, but unless you do something previous to the data step to figure out how many variables you are checking, you'll have to hard code the dimension of DF ... so why bother with the asterisks and dim function in your code ... array Am{*} m1-m11;array ADF{*} $ DF1-DF11;do j=1 to dim(AM); > >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 > >________________________________________ >From: SAS(r) Discussion [SAS-L@LISTSERV.UGA.EDU] on behalf of tony tony [tonysingsong@GMAIL.COM] >Sent: Thursday, November 10, 2011 9:36 AM >To: SAS-L@LISTSERV.UGA.EDU >Subject: arrays and nested loops > >Hi, > > > >I have 400 or more variables in the data set. For the illustration purpose >I am limiting my data code to only 11 variables. >The issue is I want to create a variable �DF� based on the next two months >balance. If the Balance/amount drops below 9 in any of the next two months >the variable � DF� gets �Y� else �N�. >I provide below the data I have and the data I want and the code that >generates error. > > > > > >Sas data creation: > > > >*Data* months; > > input m1 m2 m3 m4 m5 m6 m7 m8 m9 m10 m11; > > datalines; > > 20 20 50 50 2 30 5 100 100 40 40 > > ; > > *run*; > > > >*Data* I_wana_hav; > >Input DF1 $ m1 DF2 $ m2 DF3 $ m3 DF4 $ m4 DF5 $ m5 DF6 $ m6 DF7 $ m7 DF8 $ >m8 DF9 $ m9 DF10 $ m10 DF11 $ m11; > >Datalines; > > > >N 20 N 20 N 50 Y 50 Y 2 Y 30 Y 5 N 100 N 100 N 40 N 40 > >; > >*run*; > > > >/*code generates error*/ > >*Data* test1; > >set months; > >array Am{*} m1-m11; > >array ADF{*} $ DF1-DF11; > >do j=*1* to dim(AM); > >do i=*1* to *2*; > >if Am(j+i)<*9* then ADF{j}="y"; > >else ADF{j}="N"; > >end; > >end; > >*run*; > > > >Any help would be highly appreciated. > > > >Thanks in advance


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