Help using Base SAS procedures

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

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 5
Accepted Solution

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: 19,815

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;

View solution in original post


All Replies
Super User
Posts: 19,815

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?

IndustryYearPERSISTENT
111992
111993
111994
1119951
111996
111997
111998
111999
112000
112001
112002
112003
112004
112005
112006

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: 19,815

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.

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

Discussion stats
  • 4 replies
  • 130 views
  • 0 likes
  • 2 in conversation