Administrator
|
I thought David's method of generating a "counter" variable was rather neat. The method I typically use (MATCH FILES followed by an IF statement) is a two-liner, whereas David's is a two-liner. Here's David's method with an extension for the case where one wants a counter within each of the V1*V2 cells (or combinations).
* David Marso's method for generating a counter variable. * Posted to SPSSX-L mailing list on 30-Dec-2012 in thread "recode variable". * Simulate some data. new file. - input program. - loop ID = 1 to 1000. - compute v1 = TRUNC(UNIFORM(4))+1. - compute v2 = TRUNC(UNIFORM(4))+1. - end case. - end loop. end file. end input program. execute. ********************************************************* . * Count cases within levels of V1, ignoring V2 . sort cases by v1. * David's one-liner. COMPUTE DMcount1=SUM(1,LAG(DMcount1)*(v1 EQ LAG(v1))). * My customary two-liner. match files file = * / by v1 / first = BWcount1 . if (BWcount1 EQ 0) BWcount1 = LAG(BWcount1) + 1. ********************************************************* . * Count cases within each V1*V2 cell . sort cases by v1 v2. * David's one-liner. COMPUTE DMcount2=SUM(1,LAG(DMcount2)*(v1 EQ LAG(v1) AND v2 EQ LAG(v2))). * My customary two-liner. match files file = * / by v1 v2 / first = BWcount2 . if (BWcount2 EQ 0) BWcount2 = LAG(BWcount2) + 1. ********************************************************* . formats ID to BWcount2 (f5.0). * Display maximum value of counter1 for each level of V1, and verify results via FREQUENCIES. means DMcount1 BWcount1 by v1 / cells = max. frequencies v1. * Display maximum value of counter2 for each V1*V2 cell, and verify results via CROSSTABS. means DMcount2 BWcount2 by v1 by v2 / cells = max. CROSSTABS v1 by v2.
--
Bruce Weaver bweaver@lakeheadu.ca http://sites.google.com/a/lakeheadu.ca/bweaver/ "When all else fails, RTFM." PLEASE NOTE THE FOLLOWING: 1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above. 2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/). |
Administrator
|
There are a few other techniques which come to mind.
I post 6 here which include Bruce's and my versions previously posted. new file. - input program. - loop ID = 1 to 1000. - compute v1 = TRUNC(UNIFORM(4))+1. - compute v2 = TRUNC(UNIFORM(4))+1. - end case. - end loop. end file. end input program. ****************************. ** 1 **. COMPUTE XX=UNIFORM(1). RANK XX BY V1 V2 /RANK INTO Counter1. SORT CASES BY V1 V2 Counter1. ** 2 **. SORT CASES BY V1 V2. SPLIT FILE BY V1 V2. COMPUTE XX=1. CREATE COUNTER2=CSUM(XX). ** 3 **. SORT CASES BY V1 V2. MATCH FILES / FILE * / BY V1 V2 / FIRST=COUNTER3. IF COUNTER3=0 COUNTER3=LAG(COUNTER3) + 1. ** 4 **. SORT CASES BY V1 V2. COMPUTE Counter4=SUM(1,LAG(Counter4)*(V1 EQ LAG(V1) AND V2 EQ LAG(V2))). ** 5 **. SORT CASES BY V1 V2. IF $CASENUM EQ 1 OR V1 NE LAG(V1) OR V2 NE LAG(V2) Counter5=1. IF MISSING(Counter5) Counter5=LAG(Counter5)+1. ** 6 **. SORT CASES BY V1 V2. IF ANY(1,$CASENUM,V1 NE LAG(V1),V2 NE LAG(V2)) Counter6=1. IF MISSING(Counter6) Counter6=LAG(Counter6)+1. *******. EXE.
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me. --- "Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis." Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?" |
Many thanks to both of you. These are really helpful.
Regards, TQ -----Original Message----- From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of David Marso Sent: 31 January 2012 14:13 To: [hidden email] Subject: Re: David's method of generating a "counter" variable There are a few other techniques which come to mind. I post 6 here which include Bruce's and my versions previously posted. new file. - input program. - loop ID = 1 to 1000. - compute v1 = TRUNC(UNIFORM(4))+1. - compute v2 = TRUNC(UNIFORM(4))+1. - end case. - end loop. end file. end input program. ****************************. ** 1 **. COMPUTE XX=UNIFORM(1). RANK XX BY V1 V2 /RANK INTO Counter1. SORT CASES BY V1 V2 Counter1. ** 2 **. SORT CASES BY V1 V2. SPLIT FILE BY V1 V2. COMPUTE XX=1. CREATE COUNTER2=CSUM(XX). ** 3 **. SORT CASES BY V1 V2. MATCH FILES / FILE * / BY V1 V2 / FIRST=COUNTER3. IF COUNTER3=0 COUNTER3=LAG(COUNTER3) + 1. ** 4 **. SORT CASES BY V1 V2. COMPUTE Counter4=SUM(1,LAG(Counter4)*(V1 EQ LAG(V1) AND V2 EQ LAG(V2))). ** 5 **. SORT CASES BY V1 V2. IF $CASENUM EQ 1 OR V1 NE LAG(V1) OR V2 NE LAG(V2) Counter5=1. IF MISSING(Counter5) Counter5=LAG(Counter5)+1. ** 6 **. SORT CASES BY V1 V2. IF ANY(1,$CASENUM,V1 NE LAG(V1),V2 NE LAG(V2)) Counter6=1. IF MISSING(Counter6) Counter6=LAG(Counter6)+1. *******. EXE. Bruce Weaver wrote > > I thought David's method of generating a "counter" variable was rather > neat. The method I typically use (MATCH FILES followed by an IF > statement) is a two-liner, whereas David's is a two-liner. Here's > David's method with an extension for the case where one wants a > counter within each of the V1*V2 cells (or combinations). > > > * David Marso's method for generating a counter variable. > * Posted to SPSSX-L mailing list on 30-Dec-2012 in thread "recode > variable". > > * Simulate some data. > new file. > - input program. > - loop ID = 1 to 1000. > - compute v1 = TRUNC(UNIFORM(4))+1. > - compute v2 = TRUNC(UNIFORM(4))+1. > - end case. > - end loop. > end file. > end input program. > execute. > > ********************************************************* . > * Count cases within levels of V1, ignoring V2 . > sort cases by v1. > * David's one-liner. > COMPUTE DMcount1=SUM(1,LAG(DMcount1)*(v1 EQ LAG(v1))). > * My customary two-liner. > match files file = * / by v1 / first = BWcount1 . > if (BWcount1 EQ 0) BWcount1 = LAG(BWcount1) + 1. > ********************************************************* . > * Count cases within each V1*V2 cell . > sort cases by v1 v2. > * David's one-liner. > COMPUTE DMcount2=SUM(1,LAG(DMcount2)*(v1 EQ LAG(v1) AND v2 EQ LAG(v2))). > * My customary two-liner. > match files file = * / by v1 v2 / first = BWcount2 . > if (BWcount2 EQ 0) BWcount2 = LAG(BWcount2) + 1. > ********************************************************* . > > formats ID to BWcount2 (f5.0). > > * Display maximum value of counter1 for each level of V1, > and verify results via FREQUENCIES. > means DMcount1 BWcount1 by v1 / cells = max. > frequencies v1. > > * Display maximum value of counter2 for each V1*V2 cell, > and verify results via CROSSTABS. > means DMcount2 BWcount2 by v1 by v2 / cells = max. > CROSSTABS v1 by v2. > -- View this message in context: http://spssx-discussion.1045642.n5.nabble.com/David-s-method-of-generating-a-counter-variable-tp5442896p5444626.html Sent from the SPSSX Discussion mailing list archive at Nabble.com. ===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD ===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD |
Free forum by Nabble | Edit this page |