How can I get 0's for a variable before an observation and 1's after?

Solved
Occasional Contributor
Posts: 5

How can I get 0's for a variable before an observation and 1's after?

My data is a panel of industry-years. Some industry-years have a value of 1 for an indicator variable "PERSISTENT." (Otherwise, PERSISTENT is missing.) Some industries do not have any years for which PERSISTENT is 1, and the years in which PERSISTENT takes on a value of 1 may vary by industry.

How can I recode PERSISTENT such that it takes on values of 0 before the industry-year observation that currently has a 1 AND takes on values of 1 for all industry-years afterward?

Accepted Solutions
Solution
‎02-23-2013 09:24 PM
Super User
Posts: 23,708

Re: How can I get 0's for a variable before an observation and 1's after?

data want;

by industry;

retain flag;

if first.industry then flag=0;

if persistent=1 then flag=1;

run;

All Replies
Super User
Posts: 23,708

Re: How can I get 0's for a variable before an observation and 1's after?

Show what your data looks like, is it long or wide?

Occasional Contributor
Posts: 5

Re: How can I get 0's for a variable before an observation and 1's after?

 Industry Year PERSISTENT 11 1992 11 1993 11 1994 11 1995 1 11 1996 11 1997 11 1998 11 1999 11 2000 11 2001 11 2002 11 2003 11 2004 11 2005 11 2006

That's an example of what the data looks like. I guess that's long?

Solution
‎02-23-2013 09:24 PM
Super User
Posts: 23,708

Re: How can I get 0's for a variable before an observation and 1's after?

data want;

by industry;

retain flag;

if first.industry then flag=0;

if persistent=1 then flag=1;

run;

Occasional Contributor
Posts: 5

Re: How can I get 0's for a variable before an observation and 1's after?

Thank you so much. I was vastly overthinking it (thought I needed to do an array).

🔒 This topic is solved and locked.