I have data about the working time in12 months of 1000 workers,
id m1 m2 m3 m4 m5 m6 m7 m8 m9 m10 m11 m12;
1 30 50............................................................30
2 .........................................................................
.
.
1000....................................................................
I want to get the first month that total working time over 250, if after 6 month , the first worker have 610 hours ,then the first monthis 6
I need to create a new variable(First_month) for this value.I have tried retain and do until ,but always have problem.
do you give me some help, thanks very much!
You don't need retain, you need an array and a do loop. Your requirements are unclear as you mention 250 hours and 610. Please clarify.
In the mean time try something like the following (untested)
data want;
set have;
array mths(12) m1-m12;
total_time=0;
do i=1 to 12 while(sum<250);
total_time=sum(total_time, mths(i));
if total_time>250 then first_month=i;
end;
run;
You don't need retain, you need an array and a do loop. Your requirements are unclear as you mention 250 hours and 610. Please clarify.
In the mean time try something like the following (untested)
data want;
set have;
array mths(12) m1-m12;
total_time=0;
do i=1 to 12 while(sum<250);
total_time=sum(total_time, mths(i));
if total_time>250 then first_month=i;
end;
run;
sorry for the mistake, it is 250, not 610,
and i have do the same as you do ,
but it doesn't work ,because, if the 6th month have overed 250, then the 7th will also over 250, then all the first_month will be 12.
I have fixed it by combining "do until" with your procedures , you help me so much, thank you sincerely .
It would help understand details of your problem if you included several complete rows of data and what the final outcome should look like for that example data.
ok,
just like
id m1 m2 m3 m4 m5 m6 m7 m8 m9 m10 m11 m12
1 20 30 40 34 60 70 80 30 60 70 90 40
2 10 24 40 20 40 30 60 20 50 30 30 30
and more obs
mu require result
id m1 m2 m3 m4 m5 m6 m7 m8 m9 m10 m11 m12 First_month_over250
1 20 30 40 34 60 70 80 30 60 70 90 40 6
2 10 24 40 20 40 30 60 20 50 30 30 30 8
and more obs
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.
Early bird rate extended! Save $200 when you sign up by March 31.
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.