```Date: Wed, 28 Nov 2007 08:58:04 -0500 Reply-To: Ed Heaton Sender: "SAS(r) Discussion" From: Ed Heaton Subject: Re: Numbering Sub groups Comments: To: Randy In-Reply-To: <200711270305.lAQM3pT3013714@malibu.cc.uga.edu> Content-Type: text/plain; charset="us-ascii" Randy; You got some solutions that involved sorting the data. These are valid solutions and will work - if your dataset is not too big and you don't mind the new order. However, I don't like to sort my data if it isn't necessary. Here's a one-pass solution (if you are running SAS 9.1) that uses a hash table. /* Create your test data. */ Data test ; Input ID @7 VarA \$1. ; Cards4 ; 1 A 1 A 1 B 1 A 1 A 2 A 2 B 2 A ;;;; /* Add the subgroups. */ Data Subgroups ; Set test ; Length Group_N 8 ; If ( _N_ eq 1 ) then do ; /* Set up the hash table just one time. */ Declare hash h() ; h.defineKey('ID','VarA') ; h.defineData('Group_N') ; h.defineDone() ; End ; If h.check() then do ; /* If the key is not in the hash table, set Group_N to 1 and add the key. */ Group_N = 1 ; h.add() ; End ; Else do ; /* If the key was not in the hash table, return the data value to the Group_N variable, increment it, and put the incremented value back in the hash table. */ h.find() ; Group_N = Group_N + 1 ; If h.replace() then putLog 'ERROR: Add failed.' ; End ; Run ; Proc print ; Run ; Ed Edward Heaton, Senior Systems Analyst, Westat (An Employee-Owned Research Corporation), 1650 Research Boulevard, TB-286, Rockville, MD 20850-3195 Voice: (301) 610-4818 Fax: (301) 294-2085 mailto:EdHeaton@Westat.com http://www.Westat.com -----Original Message----- From: owner-sas-l@listserv.uga.edu [mailto:owner-sas-l@listserv.uga.edu] On Behalf Of Randy Sent: Monday, November 26, 2007 10:05 PM To: SAS-L@LISTSERV.UGA.EDU Cc: SUBSCRIBE SAS-L Anonymous Subject: Numbering Sub groups Dear All: Here is my data set: ID VarA 1 A 1 A 1 B 1 A 1 A 2 A 2 B 2 A and so on. I want to create and number the subgroups of VarA by the ID number. So my data set should look like this: ID VarA Group_N 1 A 1 1 A 2 1 B 1 1 A 3 1 A 4 2 A 1 2 B 1 2 A 2 Please help. Randy ```

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