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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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