I have a dataset of 25000 records with this layout;
ID DOC25 DOC26 DOC27 DOC28 DOC29 ........ DOC48
1 0 1 1 0 1 0
DOC25-DOC48 are montly counts which have 0 if the person was on the streets, 1 if incarcerated
DOC25 corresponds to January 2013
DOC48 corresponds to December 2014
I want to count how many times was a person incarcerated but if the person remains in prison longer than 1 month, the following months will be 1 as well until it changes to 0 when release, here in December 2014. So I want to keep track only of the first time the value changes
thanks!
The logic is if the variable value doesn't equal the previous value. You've had previous answers that demonstrate a loop over the values
and declaring an array so this should be enough to get started.
If doc(i) ne doc(i-1) then flag_count+1;
Sounds like this should do it within a DATA step:
array docs {*} doc25-doc48;
incarcerated=0;
do i=2 to 24;
if docs{i}=1 and docs{i-1}=0 then incarcerated + 1;
end;
This assumes that if DOC25=1 then you don't want to count it as an incarceration. If that's wrong, it's easy enough to change.
@Astounding wrote:
This assumes that if DOC25=1 then you don't want to count it as an incarceration. If that's wrong, it's easy enough to change.
The alternative would require changing
incarcerated=0;
to
incarcerated=docs{1};
As an alternative:
data want; doc1=0; doc2=1; doc3=1; doc4=0; doc5=1; test=count(cats(of doc:),"01"); run;
Or again, if you want to count initial incarcerations
data want;
doc1=0; doc2=1; doc3=1; doc4=0; doc5=1;
test=count(cats("0", of doc:),"01");
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.