CJ12
CJ12

Reputation: 495

Coding ranges in Stata

I was given a dataset that is VERY crude. One dataset gives me a code for each X variable (a 4 digit code) and then a text file that explains what each code means. Weirdly a code can mean something or a range could mean the same thing. For example:

X
2321
2322
2341
2520
2572
4000
4001
4002
4100

The text file is this way:

2300-2372 = New York
2520      = Chicago
2572      = Denver
4000-4099 = Austin
4100-4200 = San Diego

I wanted an easy way to code the Stata file given without creating 100's of lines of codes. Since I have over 1000 classifications and about one million observations.

Upvotes: 1

Views: 239

Answers (1)

CJ12
CJ12

Reputation: 495

Use inrange:

gen austin = 0
replace austin = 1 if inrange(X, 4000,4099)

For the, here cities, that have one code then make the range start and stop at the same point

Upvotes: 1

Related Questions