Quantcast
Channel: Statalist
Viewing all articles
Browse latest Browse all 65060

Pick the first nonmissing (and non-zero) value and create new variable

$
0
0
Hello all,

I am using Stata/IC 12.1.

I have a panel data set and I need to pick the first nonmissing (and non-zero) value of the leverage variable of firms. This value is to used as a new variable "initial leverage" for each firm.
My data looks like:

id year leverage
1 1980 .
1 1981 .
1 1982 2
2 1980 5
2 1982 6
2 1983 1
3 1980 4
3 1983 .
3 1984 2

The desired result would be:
id inilev
1 2
1 2
1 2
2 5
2 5
2 5
3 4
3 4
3 4


Comments:
- years may be different within group of firms, but they do not repeat
- regardless of the magnitude, I need to get always the first nonmissing value for each firm
- the series spams from 1950-200 for over 400 firms

Previous attemps:
Searching the archives I found similar codes, but was not able to adapt to my issue.
- one case picked the minimum value
Code:
 by id, sort: egen inilev = min(cond(!missing(leverage), leverage, .))
Thank you so much,
Clarice

Viewing all articles
Browse latest Browse all 65060

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>