|
Hari ,
Well I wasnt sure how you wanted to handle those cases but since you
clarified and my code is easy to mod:
Data Need ( Drop = Count SumVar1 Sumvar2 ) ;
Set Test ;
By Pat Treat Visit ;
If ( Visit = 1 And ( Var1 And Var2 ) ) Then Do ;
Count + 1 ;
SumVar1 + Var1 ;
SumVar2 + Var2 ;
End ;
If ( Last.Visit and Visit = 1 ) Then Do ;
Var1 = ( SumVar1 / Count ) ;
Var2 = ( SumVar2 / Count ) ;
Output;
Count = 0 ;
SumVar1 = 0 ;
SumVar2 = 0 ;
End ;
Else If Visit Ne 1 Then Output ;
Run ;
Toby Dunn
To sensible men, every day is a day of reckoning. ~John W. Gardner
The important thing is this: To be able at any moment to sacrifice that
which we are for what we could become. ~Charles DuBois
Don't get your knickers in a knot. Nothing is solved and it just makes you
walk funny. ~Kathryn Carpenter
From: Hari Nath <hari_s_nath@YAHOO.COM>
Reply-To: Hari Nath <hari_s_nath@YAHOO.COM>
To: SAS-L@LISTSERV.UGA.EDU
Subject: Re: means first. -- wonder if this is possible in a single
datastep
Date: Mon, 22 Jan 2007 10:34:04 -0500
Thanks Jo and Toby for your answers......i added the missing values in the
input dataset intentionally to make sure they come out right.....but i
think i know a way to make it disappear, but again which is the right way
to do that....Toby, i do know the solution using sql/ means and also
means/ sql may be the safest, but this is just to try with first. in one
datastep....
both Jo's and your solution has some problem....Jo said to add IF VISIT=1
AND NOT LAST.VISIT THEN DELETE; at the end of step, but my last.visit is
missing in visit 1 for the second patient....so, how should i safely
handle missing values here.....and Toby, your code calculates the mean for
the second patient which should be just 80 and 90 instead of 26.67 and 30
and as you said if we do this by sql/ means this is going to calculate for
just non missing values which would result in 80 and 90...
please advise...
Thanks again for your solution....
hari
On Mon, 22 Jan 2007 15:15:17 +0000, toby dunn <tobydunn@HOTMAIL.COM> wrote:
>Data Need ( Drop = Counter SumVar1 SumVar2 ) ;
>Set Test ;
>By Pat Treat Visit ;
>
>If Visit = 1 Then Do ;
> Count + 1 ;
> SumVar1 + Var1 ;
> SumVar2 + Var2 ;
>End ;
>
>If ( Last.Visit and Visit = 1 ) Then Do ;
> Var1 = ( SumVar1 / Count ) ;
> Var2 = ( SumVar2 / Count ) ;
> Output;
> Count = 0 ;
> SumVar1 = 0 ;
> SumVar2 = 0 ;
>End ;
>Else If ( Visit Ne 1 ) Then Output ;
>
>Run ;
>
>
>
>
>You may be just as well off by using SQL or Proc MEans or proc Summary and
>get the mean for visit 1 and then interleaving that with the original data
>where visit ne 2.
>
>
>
>
>Toby Dunn
>
>To sensible men, every day is a day of reckoning. ~John W. Gardner
>
>The important thing is this: To be able at any moment to sacrifice that
>which we are for what we could become. ~Charles DuBois
>
>Don't get your knickers in a knot. Nothing is solved and it just makes you
>walk funny. ~Kathryn Carpenter
>
>
>
>
>
>
>From: Hari Nath <hari_s_nath@YAHOO.COM>
>Reply-To: Hari Nath <hari_s_nath@YAHOO.COM>
>To: SAS-L@LISTSERV.UGA.EDU
>Subject: means first. -- wonder if this is possible in a single datastep
>Date: Mon, 22 Jan 2007 10:04:05 -0500
>
>Hi all,
> iam trying to get the means for only visit 1 and then trying to keep
>this in a same datastep.....can someone point me in right way...
>Many thanks
>hari
>
> >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>data test ;
> input pat $ treat $ visit var1 var2 ;
> cards ;
> 1000 A 1 100 90
> 1000 A 1 70 50
> 1000 A 1 30 50
> 1000 A 2 20 30
> 1000 A 2 50 60
> 1000 A 2 40 20
> 1000 A 3 50 30
> 1000 A 3 60 30
> 2000 B 1 80 90
> 2000 B 1 . .
> 2000 B 1 . .
> 2000 B 2 30 30
> 2000 B 2 40 60
> 2000 B 2 80 20
> 2000 B 3 30 30
> 2000 B 3 40 30
> ;
>run ;
>
>proc sort data=test ; by pat treat visit ; run ;
>
>data test2 ;
> retain var1_b var2_b ;
> set test ;
> by pat treat visit ;
>
> if visit=1 then do ;
>
>
> if first.visit then do ;
> var1_b = 0 ;
> var2_b = 0 ;
> counter=0;
> end ;
> var1_b = sum(var1 + var1_b) ;
> var2_b = sum(var2 + var2_b) ;
> counter + 1 ;
>
> if last.visit then do ;
> var1_b = round(var1_b / counter,.01) ;
> var2_b = round(var2_b / counter,.01) ;
> end ;
>
> end ;
>
> else if visit ^= 1 then do ;
> var1_b = var1 ;
> var2_b = var2 ;
> counter=0 ;
>
> end ;
>/* if last.visit ne . then output ;*/
>run ;
> >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>
>current output ;
>
>pat treat visit var1 var2 var1b var2b
>
> 1000 A 1 100 90 100 90
> 1000 A 1 70 50 170 140
> 1000 A 1 30 50 66.67 63.33
> 1000 A 2 20 30 20 30
> 1000 A 2 50 60 50 60
> 1000 A 2 40 20 40 20
> 1000 A 3 50 30 50 30
> 1000 A 3 60 30 60 30
> 2000 B 1 80 90 80 90
> 2000 B 1 . . . .
> 2000 B 1 . . . .
> 2000 B 2 30 30 30 30
> 2000 B 2 40 60 40 60
> 2000 B 2 80 20 80 20
> 2000 B 3 30 30 30 30
> 2000 B 3 40 30 40 30
>
> >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>
>desired output ;
>
>pat treat visit var1 var2 var1b var2b
>
> 1000 A 1 30 50 66.67 63.33
> 1000 A 2 20 30 20 30
> 1000 A 2 50 60 50 60
> 1000 A 2 40 20 40 20
> 1000 A 3 50 30 50 30
> 1000 A 3 60 30 60 30
> 2000 B 1 80 90 80 90
> 2000 B 2 30 30 30 30
> 2000 B 2 40 60 40 60
> 2000 B 2 80 20 80 20
> 2000 B 3 30 30 30 30
> 2000 B 3 40 30 40 30
>
> >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>
>_________________________________________________________________
>Get Hilary Duff?s homepage with her photos, music, and more.
>http://celebrities.live.com
_________________________________________________________________
Get Hilary Duff’s homepage with her photos, music, and more.
http://celebrities.live.com
|