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;
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;
Thanks for the code. If I want to stop the count at 525, what do I change?
proc sql;
select
count(distinct week) as weeks
from have;
quit;
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
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;
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!
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.
Ready to level-up your skills? Choose your own adventure.