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 (April 2004, week 5)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:         Wed, 28 Apr 2004 22:17:05 -0700
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: writing efficient code - macro???
Comments: To: Noreen McDonald <noreen@BERKELEY.EDU>
In-Reply-To:  <web-2361318@calmail-ma.berkeley.edu>
Content-Type: text/plain; charset=us-ascii

--- Noreen McDonald <noreen@BERKELEY.EDU> wrote: > Being at the beginning of the SAS learning curve, I have a problem > which I've seen a few times. > There are many times I want to use a counter variable in multiple > ways, ie to identify position > in matrix or a particular variable. I've only been able to solve it > inelegantly using wallpaper > code. If anyone could show me how to do it better, I would greatly > appreciate it. It's probably > best explained by an example. Noreen > data test;/*wallpaper approach*/ > array dummy(*) dummy1-dummy5; > set temp; > if var1=1 then dummy(1)=num1; > if var2=2 then dummy(2)=num2; > /*And so on... You get the idea ...*/ > run; > What I would like is to create a counter variable which I could use > like so: > data test;/*just an idea of what I want, code doesn't work*/ > array dummy(*) dummy1-dummy5; > do i=1 to 5; > set temp; > if var&i=i then dummy(i)=num&i; > /* is there a way to use counters to identify variables ,e.g. var1, > var2*/ > run; >

Noreen,

There is no need for macro code here. A macro could be used to write your "wallpaper" code. That could make it a little easier to write all of the assignment statements. However, it does nothing to reduce the complexity of the code.

The simple solution is to declare arrays for var1-var5 and n1-n5 along with the array for dummy1-dummy5. You can loop over a datastep variable i to index the array positions.

data test; array dummy {5} dummy1-dummy5; array var {5} var1-var5; array n {5} n1-n5; set temp; do i=1 to 5; if var{i}=i then dummy(i)=num{i}; end; run;

Dale

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

__________________________________ Do you Yahoo!? Win a $20,000 Career Makeover at Yahoo! HotJobs http://hotjobs.sweepstakes.yahoo.com/careermakeover


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