Help using Base SAS procedures

output the first month that working time over 250 hours

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 7
Accepted Solution

output the first month that working time over 250 hours

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!


Accepted Solutions
Solution
‎12-11-2015 04:33 AM
Super User
Posts: 19,771

Re: output the first month that working time over 250 hours

[ Edited ]

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;

View solution in original post


All Replies
Solution
‎12-11-2015 04:33 AM
Super User
Posts: 19,771

Re: output the first month that working time over 250 hours

[ Edited ]

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;
Occasional Contributor
Posts: 7

Re: output the first month that working time over 250 hours

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.

Occasional Contributor
Posts: 7

Re: output the first month that working time over 250 hours

I have fixed it  by combining "do until" with your procedures , you help me so much, thank you sincerely .

Super User
Posts: 11,343

Re: output the first month that working time over 250 hours

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.

Occasional Contributor
Posts: 7

Re: output the first month that working time over 250 hours

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 

 

 

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 341 views
  • 0 likes
  • 3 in conversation