Announcement

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

  • Codes to store values in STATA

    I have state data that I want to store using their FIPS code numbers in numerical format. However, when I view the data, I also want to see the state names instead of just the numbers. How can I code this to store the values? I have the FIPS code values in numerical format and the state names in string format.

  • #2
    If you already have FIPS and state names, then
    Code:
    save fipsnames, replace

    Comment


    • #3
      Code:
      preserve
      keep fips_num fips_str
      duplicates drop
      isid fips_num, sort
      forvalues i = 1/`=_N' {
          label define fips_label `=fips_num[`i']' `"`=fips_str[`i']'"', add
      }
      label value fips_num, fips_label
      tempfile labeler
      label save fips_label using `labeler'
      
      restore
      run `labeler'
      label values fips_num fips_label
      Here I assume you have a single data set that contains both a numeric fips_code variable (fips_num) and in the same observations the corresponding state names in a string variable named fips_str. You can modify the above code to use the actual names for those variables in your real data set. (And in the future, please show example data to eliminate the need for imaginary data leading to imaginary code that you must then modify to suit reality. Use -dataex- to do that.)

      Added: Crossed with #2.

      Also added: By the way, Daniel Klein's -elabel- package, available from SSC, has a command that will do this, but the syntax is a bit complicated, I don't remember it off hand, and I don't have the time to re-create it right now.

      Comment


      • #4
        Hello, Thank you for your response. I used the following code for my problem:

        Code:
        label variable State "State Name"
        label values State statelabel
        label define statelabel 1 "Alabama" 2 "Alaska" 4 "Arizona" 5 "Arkansas" 6 "California" 8 "Colorado" 9 "Connecticut" 10 "Delaware" 12 "Florida" 13 "Georgia" 15 "Hawaii" 16 "Idaho" 17 "Illinois" 18 "Indiana" 19 "Iowa" 20 "Kansas" 21 "Kentucky" 22 "Louisiana" 23 "Maine" 24 "Maryland" 25 "Massachusetts" 26 "Michigan" 27 "Minnesota" 28 "Mississippi" 29 "Missouri" 30 "Montana" 31 "Nebraska" 32 "Nevada" 33 "New Hampshire" 34 "New Jersey" 35 "New Mexico" 36 "New York" 37 "North Carolina" 38 "North Dakota" 39 "Ohio" 40 "Oklahoma" 41 "Oregon" 42 "Pennsylvania" 44 "Rhode Island" 45 "South Carolina" 46 "South Dakota" 47 "Tennessee"  48 "Texas" 49 "Utah" 50 "Vermont" 51 "Virginia" 53 "Washington" 54 "West Virginia" 55 "Wisconsin" 56 "Wyoming"
        label values State statelabel

        I was using the following codes for a very long time and it was not working.

        Code:
        label define statelabel 1 "Alabama" 2 "Alaska" 4 "Arizona" 5 "Arkansas" 6 "California" ///
        8 "Colorado" 9 "Connecticut" 10 "Delaware" 12 "Florida" 13 "Georgia" ///
        15 "Hawaii" 16 "Idaho" 17 "Illinois" 18 "Indiana" 19 "Iowa" ///
        20 "Kansas" 21 "Kentucky" 22 "Louisiana" 23 "Maine" 24 "Maryland" ///
        25 "Massachusetts" 26 "Michigan" 27 "Minnesota" 28 "Mississippi" ///
        29 "Missouri" 30 "Montana" 31 "Nebraska" 32 "Nevada" 33 "New Hampshire" ///
        34 "New Jersey" 35 "New Mexico" 36 "New York" 37 "North Carolina" ///
        38 "North Dakota" 39 "Ohio" 40 "Oklahoma" 41 "Oregon" 42 "Pennsylvania" ///
        44 "Rhode Island" 45 "South Carolina" 46 "South Dakota" 47 "Tennessee" ///
        48 "Texas" 49 "Utah" 50 "Vermont" 51 "Virginia" 53 "Washington" ///
        54 "West Virginia" 55 "Wisconsin"
        56 "Wyoming"
        Can anyone tell me why it wasn't working? I was getting this sort of message like "54 is not a valid command name"..

        Thanks!

        Comment


        • #5
          Code:
          clear
          input str2 stfips str20 statename    str2    stateabbr
          "01" "Alabama"              "AL"
          "02" "Alaska"               "AK"
          "04" "Arizona"              "AZ"
          "05" "Arkansas"             "AR"
          "06" "California"           "CA"
          "08" "Colorado"             "CO"
          "09" "Connecticut"          "CT"
          "10" "Delaware"             "DE"
          "11" "District of Columbia" "DC"
          "12" "Florida"              "FL"
          "13" "Georgia"              "GA"
          "15" "Hawaii"               "HI"
          "16" "Idaho"                "ID"
          "17" "Illinois"             "IL"
          "18" "Indiana"              "IN"
          "19" "Iowa"                 "IA"
          "20" "Kansas"               "KS"
          "21" "Kentucky"             "KY"
          "22" "Louisiana"            "LA"
          "23" "Maine"                "ME"
          "24" "Maryland"             "MD"
          "25" "Massachusetts"        "MA"
          "26" "Michigan"             "MI"
          "27" "Minnesota"            "MN"
          "28" "Mississippi"          "MS"
          "29" "Missouri"             "MO"
          "30" "Montana"              "MT"
          "31" "Nebraska"             "NE"
          "32" "Nevada"               "NV"
          "33" "New Hampshire"        "NH"
          "34" "New Jersey"           "NJ"
          "35" "New Mexico"           "NM"
          "36" "New York"             "NY"
          "37" "North Carolina"       "NC"
          "38" "North Dakota"         "ND"
          "39" "Ohio"                 "OH"
          "40" "Oklahoma"             "OK"
          "41" "Oregon"               "OR"
          "42" "Pennsylvania"         "PA"
          "44" "Rhode Island"         "RI"
          "45" "South Carolina"       "SC"
          "46" "South Dakota"         "SD"
          "47" "Tennessee"            "TN"
          "48" "Texas"                "TX"
          "49" "Utah"                 "UT"
          "50" "Vermont"              "VT"
          "51" "Virginia"             "VA"
          "53" "Washington"           "WA"
          "54" "West Virginia"        "WV"
          "55" "Wisconsin"            "WI"
          "56" "Wyoming"              "WY"
          end

          Comment


          • #6
            you can destring stfips if you'd like.

            save the file somewhere handy. you'll probably need it again in the future.

            Comment


            • #7
              Hello,

              I just wanted to know why using /// is not working in my STATA. As far as I know, /// is the continuation character at the end of each line, which continues the command. When I am writing it in one line it works!

              Thanks!

              Comment


              • #8
                In the code shown in #4, the problem is that you don't have a /// at the end of the penultimate line. So Stata thinks that the command ends there, and what it encounters next is not a valid start of a new command.

                Regarding #7, you are correct that /// is a continuation "character" at the end of each line. But only in a do-file or ado-file. If you try to use it when you type commands into the Command window for immediate execution, it does not work there.

                Comment


                • #9
                  Here's another approach. labmask is from the Stata Journal.

                  Code:
                  . search labmask, sj
                  
                  Search of official help files, FAQs, Examples, and Stata Journals
                  
                  SJ-8-2  gr0034  . . . . . . . . . .  Speaking Stata: Between tables and graphs
                          (help labmask, seqvar if installed) . . . . . . . . . . . .  N. J. Cox
                          Q2/08   SJ 8(2):269--289
                          outlines techniques for producing table-like graphs


                  Code:
                  clear
                  input str2 stfips str20 statename    str2    stateabbr
                  "01" "Alabama"              "AL"
                  "02" "Alaska"               "AK"
                  "04" "Arizona"              "AZ"
                  "05" "Arkansas"             "AR"
                  "06" "California"           "CA"
                  "08" "Colorado"             "CO"
                  "09" "Connecticut"          "CT"
                  "10" "Delaware"             "DE"
                  "11" "District of Columbia" "DC"
                  "12" "Florida"              "FL"
                  "13" "Georgia"              "GA"
                  "15" "Hawaii"               "HI"
                  "16" "Idaho"                "ID"
                  "17" "Illinois"             "IL"
                  "18" "Indiana"              "IN"
                  "19" "Iowa"                 "IA"
                  "20" "Kansas"               "KS"
                  "21" "Kentucky"             "KY"
                  "22" "Louisiana"            "LA"
                  "23" "Maine"                "ME"
                  "24" "Maryland"             "MD"
                  "25" "Massachusetts"        "MA"
                  "26" "Michigan"             "MI"
                  "27" "Minnesota"            "MN"
                  "28" "Mississippi"          "MS"
                  "29" "Missouri"             "MO"
                  "30" "Montana"              "MT"
                  "31" "Nebraska"             "NE"
                  "32" "Nevada"               "NV"
                  "33" "New Hampshire"        "NH"
                  "34" "New Jersey"           "NJ"
                  "35" "New Mexico"           "NM"
                  "36" "New York"             "NY"
                  "37" "North Carolina"       "NC"
                  "38" "North Dakota"         "ND"
                  "39" "Ohio"                 "OH"
                  "40" "Oklahoma"             "OK"
                  "41" "Oregon"               "OR"
                  "42" "Pennsylvania"         "PA"
                  "44" "Rhode Island"         "RI"
                  "45" "South Carolina"       "SC"
                  "46" "South Dakota"         "SD"
                  "47" "Tennessee"            "TN"
                  "48" "Texas"                "TX"
                  "49" "Utah"                 "UT"
                  "50" "Vermont"              "VT"
                  "51" "Virginia"             "VA"
                  "53" "Washington"           "WA"
                  "54" "West Virginia"        "WV"
                  "55" "Wisconsin"            "WI"
                  "56" "Wyoming"              "WY"
                  end
                  
                  destring stfips, replace
                  labmask stfips, values(statename)
                  
                  l in 1/10
                  
                       +--------------------------------------------------------+
                       |               stfips              statename   statea~r |
                       |--------------------------------------------------------|
                    1. |              Alabama                Alabama         AL |
                    2. |               Alaska                 Alaska         AK |
                    3. |              Arizona                Arizona         AZ |
                    4. |             Arkansas               Arkansas         AR |
                    5. |           California             California         CA |
                       |--------------------------------------------------------|
                    6. |             Colorado               Colorado         CO |
                    7. |          Connecticut            Connecticut         CT |
                    8. |             Delaware               Delaware         DE |
                    9. | District of Columbia   District of Columbia         DC |
                   10. |              Florida                Florida         FL |
                       +--------------------------------------------------------+
                  Last edited by Nick Cox; 15 Jul 2024, 19:36.

                  Comment


                  • #10
                    Thank you all; I understand now!

                    Comment

                    Working...
                    X