DATA Step, Macro, Functions and more

counting

Reply
Frequent Contributor
Posts: 110

counting

I would like to count the number of weeks in my dataset.  The starting number should begin with 501.  Please advise on the code.  Below is a sample of dataset I want to have in the end.  The week count continues for 10 years.

Thanks

Data want;

input week count;

05Jul2010 501

05Jul2010 501

12Jul2010 502

12Jul2010 502

12Jul2010 502

19Jul2010 503

26Jul2010 504

26Jul2010 504

26Jul2010 504

26Jul2010 504

;;

run;

Super Contributor
Posts: 358

Re: counting

Posted in reply to sophia_SAS

proc sort data=want;

by week;

run;

data want2;

set want;

by week;

retain count;

if _n_=1 then count=500;

if first.week then count + 1;

run;

Frequent Contributor
Posts: 110

Re: counting

Thanks for the code.  If I want to stop the count at 525, what do I change?

Super Contributor
Posts: 578

Re: counting

Posted in reply to sophia_SAS

proc sql;

select

     count(distinct week) as weeks

from have;

quit;

Respected Advisor
Posts: 3,156

Re: counting

Posted in reply to sophia_SAS

Here is one way:

Data have;

     input week date9.;

     format week date9.;

     cards;

05Jul2010

05Jul2010

12Jul2010

12Jul2010

12Jul2010

19Jul2010

26Jul2010

26Jul2010

26Jul2010

26Jul2010

;;

/*data step*/

data want;

     set have;

     retain start;

     if _n_=1 then

           start=week;

     count=intck('week',start,week)+501;

     drop start;

run;

/*proc sql;*/

proc sql;

     create table want_sql as

           select *, 501+intck('week',min(week),week) as count from have;

quit;

Regards,

Haikuo

Trusted Advisor
Posts: 1,231

Re: counting

Posted in reply to sophia_SAS

data want;
set have;
by week;
if first.week then n+1;
run;

data final;
set want;
count=500+n;
if count>525 then stop;
run;

Ask a Question
Discussion stats
  • 5 replies
  • 277 views
  • 0 likes
  • 5 in conversation