|
Gerhard ,
Using a where clause wont work if there the data looks like:
1 1-Sep-04 0
1 2-Oct-04 1
1 5-Dec-04 63
1 9-Dec-04 18
1 1-Jan-05 -4
1 3-Mar-05 62
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: Gerhard Hellriegel <gerhard.hellriegel@T-ONLINE.DE>
Reply-To: Gerhard Hellriegel <gerhard.hellriegel@T-ONLINE.DE>
To: SAS-L@LISTSERV.UGA.EDU
Subject: Re: modified version of counting obs
Date: Mon, 26 Feb 2007 10:26:13 -0500
Think in all suggested solution you simply must have a where clause like:
where year(date)=2004 and late<=30;
This is only true, if the sub-order is by late, so you have a sort order
like:
by id late;
If not, you could add a trigger for the first late>30, like:
title 'counting observations within 2004';
data test;
input id date:date9. late;
format date date9.;
datalines;
1 1-Sep-04 0
1 2-Oct-04 1
1 5-Dec-04 63
1 1-Jan-05 -4
1 3-Mar-05 62
2 1-Jan-04 0
2 13-Feb-04 12
2 5-Mar-04 -8
2 12-Apr-05 7
3 3-Mar-04 0
3 4-Apr-04 1
4 3-Jun-04 0
5 1-Sep-04 0
5 3-Oct-04 2
5 14-Nov-04 41
5 15-Jan-05 61
5 16-Feb-05 1
;;;;
run;
%let year=2004;
data _null_;
set test;
retain counter lategt30;
by id;
if first.id then do;
counter=0;
lategt30=0;
end;
if late>30 then lategt30=1;
if year(date)=&year and not lategt30 then counter+1;
call symput("c"!!compress(put(id,8.)),counter);
call symput("maxid",id);
run;
%macro output;
data _null_;
set test;
by id;
%do i=1 %to &maxid;
if id=&i and first.id then put "&&c&i obs with id=&i in year
&year";
%end;
run;
%mend;
%output;
For the other suggested solutions you should add a data-step to set the
late-trigger and use that in a where clause, e.g. in proc summary.
Regards,
Gerhard
On Mon, 26 Feb 2007 06:42:31 -0800, Syb it <sas_datalover@YAHOO.COM> wrote:
>I have a dataset that looks like the one below. There are two conditions
I need to meet:
>
> 1) I want to count observations within a particular time period, in
this case 2004. Everything outside that boundary I want to skip.
> 2) I want to stop counting by ID once I hit late gt 30.
>
>
> So in the example data set below, for ID 1 I should have a count of 2,
for ID=2, count is 3. ID=3 count is 2, ID = 5 count is 2. So for
observations within 2004, once I hit late > 30 I want to stop counting,
and ignore the rest of the information for that ID.
>
> Any suggestions?
>
> title 'counting observations within 2004';
> data test;
> input id date:date9. late;
> format date date9.;
> datalines;
> 1 1-Sep-04 0
> 1 2-Oct-04 1
> 1 5-Dec-04 63
> 1 1-Jan-05 -4
> 1 3-Mar-05 62
> 2 1-Jan-04 0
> 2 13-Feb-04 12
> 2 5-Mar-04 -8
> 2 12-Apr-05 7
> 3 3-Mar-04 0
> 3 4-Apr-04 1
> 4 3-Jun-04 0
> 5 1-Sep-04 0
> 5 3-Oct-04 2
> 5 14-Nov-04 41
> 5 15-Jan-05 61
> 5 16-Feb-05 1
> ;;;;
> run;
> proc print data=test; run;
>
>
> Thank you.
>
>
>
>---------------------------------
>Any questions? Get answers on any topic at Yahoo! Answers. Try it now.
_________________________________________________________________
Win a Zune™—make MSN® your homepage for your chance to win!
http://homepage.msn.com/zune?icid=hmetagline
|