Good morning all! I am new to this forum and I hope that someone can help me.
I have a dataset made in this way
date animals status
1/03/2015 30000 full
1/04/2015 29950 full
1/04/2015 29905 full
1/05/2015 28880 full
1/06/2015 28870 full
. . .
. . .
. . .
2/20/2015 0 empty
2/21/2015 0 empty
2/22/2015 0 empty
. . .
. . .
2/28/2015 30000 full
ecc..
Let me explain my problem, I have to number the day each time the shed is filled with animals (and I need to stop the count when the shed is empty), but I really do not know how to do this. the file is made of several rounds (made in the same way)
I've tried some do loop including the "if first." condition, but did not work properly. I tried also some macros, but I'm not so skilled.
the final dataset should look like this:
date animals status day round
1/03/2015 30000 full 1 1
1/04/2015 29950 full 2 1
1/04/2015 29905 full 3 1
1/05/2015 28880 full 4 1
1/06/2015 28870 full 5 1
. . .
. . .
. . .
2/20/2015 0 empty . .
2/21/2015 0 empty . .
2/22/2015 0 empty . .
. . .
. . .
2/28/2015 30000 full 1 2
3/01/2015 29999 full 2 2
3/02/2015 29999 full 3 2
ecc..
data want;
set have;
retain _round 0;
prevstat = lag(status);
if status = 'empty' then do;
day = .;
round = .;
end;
else do;
if prevstat in ('empty','') then do;
_round + 1;
day = 0;
end;
round = _round;
day + 1;
end;
run;
Arrays make processing variables in observation easier, macros are for code generation and seldom a must-have to solve a problem. First/Last seem to be the appropriate tools to solve the issue, but you need to explain the logic in more detail, e.g. why is day increased when date has not changed? And posting data as data-step using datalines-statement makes it easier to work on your problem.
Sorry, the data did not change because It was a mistake.
I included a part of the file to be more clear.
data want;
set have;
retain _round 0;
prevstat = lag(status);
if status = 'empty' then do;
day = .;
round = .;
end;
else do;
if prevstat in ('empty','') then do;
_round + 1;
day = 0;
end;
round = _round;
day + 1;
end;
run;
It worked!
thank you very much!!
To me using a BY statement with NOSTORTED options is a bit easier. Same difference I reckon.
data want;
set have;
by status notsorted;
if first.status then call missing(d);
if status ne: 'em' then do;
d + 1;
if first.status then r+1;
day = d;
round = r;
end;
drop d r;
run;
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.