Date: Mon, 13 Mar 2000 13:47:53 -0800 Ya Huang "SAS(r) Discussion" Ya Huang Re: Data step problem To: "Robert Andrew Bowell Jr." text/plain; charset=us-ascii

If you don't mind to try proc sql, here is a solution:

** testing data; data test; do id=1 to 6; do j=1 to 10; corclass=int(uniform(id*j)*4)+1; output; end; end;

proc sql; create table test as select *, sum(corclass=1)/(sum(corclass=1)+sum(corclass=2)) as hitrate, sum(corclass=3)/(sum(corclass=3)+sum(corclass=4)) as flsrate from test group by id ;

proc print; run;

============================= OBS ID CORCLASS HITRATE FLSRATE

1 1 1 0.60 0.40000 2 1 3 0.60 0.40000 3 1 2 0.60 0.40000 4 1 3 0.60 0.40000 5 1 4 0.60 0.40000 6 1 4 0.60 0.40000 7 1 4 0.60 0.40000 8 1 2 0.60 0.40000 9 1 1 0.60 0.40000 10 1 1 0.60 0.40000 11 2 2 0.50 0.33333 12 2 3 0.50 0.33333 13 2 1 0.50 0.33333 14 2 4 0.50 0.33333 15 2 4 0.50 0.33333 16 2 4 0.50 0.33333 17 2 4 0.50 0.33333 18 2 3 0.50 0.33333 19 2 1 0.50 0.33333 20 2 2 0.50 0.33333 21 3 3 0.00 0.83333 22 3 2 0.00 0.83333 23 3 4 0.00 0.83333 24 3 3 0.00 0.83333 25 3 2 0.00 0.83333 26 3 3 0.00 0.83333 27 3 3 0.00 0.83333 28 3 2 0.00 0.83333 29 3 3 0.00 0.83333 30 3 2 0.00 0.83333 31 4 1 0.25 0.66667 32 4 3 0.25 0.66667 33 4 3 0.25 0.66667 34 4 2 0.25 0.66667 35 4 2 0.25 0.66667 36 4 2 0.25 0.66667 37 4 3 0.25 0.66667 38 4 3 0.25 0.66667 39 4 4 0.25 0.66667 40 4 4 0.25 0.66667 41 5 1 0.60 0.40000 42 5 1 0.60 0.40000 43 5 2 0.60 0.40000 44 5 3 0.60 0.40000 45 5 1 0.60 0.40000 46 5 3 0.60 0.40000 47 5 4 0.60 0.40000

OBS ID CORCLASS HITRATE FLSRATE

48 5 4 0.60000 0.4 49 5 2 0.60000 0.4 50 5 4 0.60000 0.4 51 6 1 0.57143 1.0 52 6 3 0.57143 1.0 53 6 3 0.57143 1.0 54 6 1 0.57143 1.0 55 6 2 0.57143 1.0 56 6 1 0.57143 1.0 57 6 2 0.57143 1.0 58 6 2 0.57143 1.0 59 6 3 0.57143 1.0 60 6 1 0.57143 1.0

HTH

Ya Huang

"Robert Andrew Bowell Jr." wrote:

> Hello, > Below is the program we're working on. I'll now explain what > we're trying to do. > We have six subjects, undergoing repeated trials, and each > subjects can have 4 possible responses, which come out under the variable > CORCLASS. So, for example, subject X would have multiple responses in > different observations, possible outcomes for CORCLASS including 1, 2, 3 > or 4. > We want to arrive at 2 variables, I'll called HITRATE and > FLSERATE. Here's the formula: > > HITRATE=(frequency of CORCLASS value 1) / ((frequency of CORCLASS value 1) > + (frequency of CORCLASS value 2)) > > AND > > FLSERATE=(frequency of CORCLASS value 3) / ((frequency of CORCLASS value > 3) + (frequency of CORCLASS value 4)) > > This is then combined back into the original data set, so that > there should only be 6 possible values for HITRATE and six others for > FLSERATE. For some reason, I'm getting 11 values for HITRATE and 13 for > FLSERATE. This seems to come about because the COUNT (frequency of > CORCLASS responses 1 and 2 (or 3 and 4, depending on which dyad we're > looking at)) variable is fluctuating in ways it shouldn't. COUNT should > only have 1 value for each subject and CORCLASS dyad (CORCLASS dyads being > 1 and 2 or 3 and 4), but as this sample of a proc print output of the > merged data set shows, that's not the case: > > 75635 6 18 1 SKH 56 5 0 6 1 0 2 0.7695 3 0.7695 1493 9107 . 0.16394 > > 75636 6 18 1 RPZ 65 5 1 6 0 0 2 0.8789 3 0.8789 1493 15228 . 0.09804 > > 75637 6 18 1 LVH 71 5 1 6 0 0 2 0.6602 3 0.6602 1493 15228 . 0.09804 > > Can you help determine the source of these fluctuations? > > =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= > > data test; > infile 'd:\temp\Da271098.dat'; > input > @1 subject 2. > @3 session 2. > @5 conditn 1. > @10 trigram \$3. > @14 trial 3. > @22 block 1. > @25 numpres 1. > @28 numint 1. > @31 stdytest 1. > @34 targdist 1. > @37 response 1. > @40 time 6. > @67 corclass 1. > @70 corrtime 6.; > run; > > data test; > set test; > if corclass=1 > then corclass=9; > else > if corclass=3 > then corclass=10; > else > if corclass=2 > then corclass=11; > else > > if corclass=0 > then corclass=12; > run; > > data test; > set test; > if corclass=9 > then corclass=1; > else > if corclass=10 > then corclass=2; > else > if corclass=11 > then corclass=3; > else > if corclass=12 > then corclass=4; > run; > > proc sort data=test; > by subject corclass; > run; > > data subset; > set test; > by subject corclass; > if first.corclass then total=0; > total + 1; > if last.corclass then output; > run; > > data test; > merge test subset; > by subject corclass; > run; > > data test2 (keep=subject corclass count); > set test; > by subject; > if first.subject then do; > count=0; > count2=0; > end; > count+total; > count2+1; > if count2=2 then do; > if corclass=2 then corclass=1; > else if corclass=4 then corclass=3; > output; > count2=0; > count=0; > end; > > run; > > data merged; > merge test test2; > by subject corclass; > run; > > data merged; > set merged; > if corclass=1 > then hitrate=total/count; > else hitrate=.; > if corclass=3 > then flserate=total/count; > else flserate=.; > run; > > proc print data=merged; > run; > > Y'obt., > Rob Bowell > ------------------------------------------------- > Programmer/Analyst, RUCS User Services Group > Hill Center, Room 209 > Phone: 732-445-0481/Fax: 732-445-5539 > -------------------------------------------------

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