Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Expanding data by a year range captured by one variable

    Hi folks,

    I have a dataset on historical fishery identifiers which has the following format:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str5 fishery str11 years str53 characteristics byte(v4 v5)
    "D 9CA" "1997 - 1998" "DUNGENESS CRAB , 150 POTS/OR 50% OF MAX, SOUTHEAST"    . .
    "D 9BA" "1997 - 2001" "DUNGENESS CRAB , 225 POTS/OR 75% OF MAX, SOUTHEAST"    . .
    "D 9AA" "1997 - 2000" "DUNGENESS CRAB , 300 POTS/OR 100% OF MAX, SOUTHEAST"   . .
    "D 12E" "1992"        "DUNGENESS CRAB , DIVE/HAND PICK, PRINCE WILLIAM SOUND" . .
    "D 12A" "1992"        "DUNGENESS CRAB , DIVE/HAND PICK, SOUTHEAST"            . .
    "D 12B" "1983 - 1985" "DUNGENESS CRAB , DIVE/HAND PICK, STATEWIDE"            . .
    end
    I would like to expand the data to look like this, with a line for each relevant year captured in the "years" variable :

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str5 fishery int years str53 characteristics
    "D 9CA" 1997 "DUNGENESS CRAB , 150 POTS/OR 50% OF MAX, SOUTHEAST"   
    "D 9CA" 1998 "DUNGENESS CRAB , 150 POTS/OR 50% OF MAX, SOUTHEAST"   
    "D 9BA" 1997 "DUNGENESS CRAB , 225 POTS/OR 75% OF MAX, SOUTHEAST"   
    "D 9BA" 1998 "DUNGENESS CRAB , 225 POTS/OR 75% OF MAX, SOUTHEAST"   
    "D 9BA" 1999 "DUNGENESS CRAB , 225 POTS/OR 75% OF MAX, SOUTHEAST"   
    "D 9BA" 2000 "DUNGENESS CRAB , 225 POTS/OR 75% OF MAX, SOUTHEAST"   
    "D 9BA" 2001 "DUNGENESS CRAB , 225 POTS/OR 75% OF MAX, SOUTHEAST"   
    "D 9AA" 1997 "DUNGENESS CRAB , 300 POTS/OR 100% OF MAX, SOUTHEAST"  
    "D 9AA" 1998 "DUNGENESS CRAB , 300 POTS/OR 100% OF MAX, SOUTHEAST"  
    "D 9AA" 1999 "DUNGENESS CRAB , 300 POTS/OR 100% OF MAX, SOUTHEAST"  
    "D 9AA" 2000 "DUNGENESS CRAB , 300 POTS/OR 100% OF MAX, SOUTHEAST"  
    "D 12E" 1992 "DUNGENESS CRAB , DIVE/HAND PICK, PRINCE WILLIAM SOUND"
    "D 12A" 1992 "DUNGENESS CRAB , DIVE/HAND PICK, SOUTHEAST"           
    "D 12B" 1983 "DUNGENESS CRAB , DIVE/HAND PICK, STATEWIDE"           
    "D 12B" 1984 "DUNGENESS CRAB , DIVE/HAND PICK, STATEWIDE"           
    "D 12B" 1985 "DUNGENESS CRAB , DIVE/HAND PICK, STATEWIDE"           
    end
    I know this is relatively easy to do via creating a range variable and using the expand command, but I am not sure how to make the year variable correctly cycle through the ranges for each permit in the new dataset.

    Let me know. Thanks folks!

  • #2
    Code:
    split years, parse(" - ") gen(yr) destring
    gen `c(obs_t)' obs_no = _n
    expand yr2-yr1+1
    by obs_no (yr1), sort: gen year = yr1[1] + _n - 1, after(years)
    drop yr*

    Comment


    • #3
      This works perfectly. Thank you Clyde!

      Comment

      Working...
      X