Date: Tue, 27 Jun 2000 12:12:27 +0100
Reply-To: Peter Crawford <peter.crawford@DB.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Peter Crawford <peter.crawford@DB.COM>
Subject: Re: Creating dummy vars (lots of them)
Content-type: text/plain; charset=us-ascii
since you start with data, and you want some data, I thought it might be easier to operate in that context rather than some complex macro looping confusion (well they confuse me!)
Of course I can't escape completely from the macro world, and there are a couple of macro variables in this solution
Here I offer a mixture of sql and data step
first some very simple data
data your.dataset;
input id @@ ;
cards;
1 23 54 98 123121 75 43 23 75 54 19 19 23 1 23 123121 75 1
;;;
now I have clipped the log, to prove the principle seems to work in this test mode
The test platform is v6.12 on winNT
243 /* put number of unique ID values into macro var &numID */
244 proc sql ;
244 /* easier than sorting! */
245 /* when unsorted and no index available */
246 create index id on your.dataset (id) ;
NOTE: Simple index ID has been defined.
247
248 select distinct id into :IDs separated by ' '
249 from your.dataset;
249 /* put unique IDs into a list */
250 %let numID = &sqlobs; /* number of unique IDs */
251 %put info: Found &numID unique values of ID;
info: Found 8 unique values of ID
252 quit;
NOTE: The PROCEDURE SQL used 0.51 seconds.
now the data step to generate a dataset with the dummy variables
253
254 data withdums; /* the new dataset with dummy variables **/
255 set your.dataset;
256 by id; /* access your data in ID order, to
257 establish the dummy value more easily */
258 array dum( &numID ) ; /* setup and access the dummy
259 vars with this array */
260 retain dum1 - dum&numID 0
261 Idum 0 ;
262 drop Idum ;
263
264 if first.id then
265 do;
266 Idum +1 ; /* up pointer in dummy array */
267 dum( Idum ) =1 ; /* and set dummy variable */
268 end;
269
270 output ; /* release obs with only one dummy set to 1 */
271
272 if last.id then dum( idum ) = 0 ;
273 /* clear that dummy, only when finished with ID */
274 run;
NOTE: The data set WORK.WITHDUMS has 18 observations and 9 variables.
NOTE: The DATA statement used 0.29 seconds.
There is probably some simple way to generate labels for each of the dummy variables to indicate the ID value each represents.
I can't promise that the performance with large data volumes will be acceptable, but it may be worth trying !
Regards
Peter Crawford
Datum: 27/06/2000 07:13
An: SAS-L@listserv.uga.edu
Antwort an: daver80@dakotacom.net
Betreff: Creating dummy vars (lots of them)
Nachrichtentext:
Hi,
I'm trying to create a very large set of dummy variables from a
single grouping variable which takes on nearly 1000 unique values. Each
dummy corresponds to a unique value of the grouping variable. The dummy
needs to equal 1 when the group var equals the corresponding value for the
dummy var and 0 otherwise. The example below illustrates what I mean.
id dum1 dum2 dum3
1 1 0 0
2 0 1 0
2 0 1 0
3 0 0 1
3 0 0 1
I have two questions:
1) is there an easy way to do this?
I've thought about trying to do this with macros, but my problem is that
I want SAS to figure out how many dummies it needs using the 'max' option in
proc univariate (i.e. what's the largest value of "id"?) and then pass that
number to a macro so it can execute a loop to create the dummies one by one.
I have no idea how to do this.
2) I'm essentially trying to do a fixed effects model (w/ logistic reg.),
is there a better way to do this in SAS?
daver
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 80,000 Newsgroups - 16 Different Servers! =-----