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
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.