BookmarkSubscribeRSS Feed
sophia_SAS
Obsidian | Level 7

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;

5 REPLIES 5
OS2Rules
Obsidian | Level 7

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;

sophia_SAS
Obsidian | Level 7

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

DBailey
Lapis Lazuli | Level 10

proc sql;

select

     count(distinct week) as weeks

from have;

quit;

Haikuo
Onyx | Level 15

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

stat_sas
Ammonite | Level 13

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;

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1118 views
  • 0 likes
  • 5 in conversation