Announcement

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

  • problem creating variable


    This should be simple but I can't get it to work. I have a data set attached in which I want to create a variable for US states. The state variables in the World Values Study are X048ISO and X048WVS.I created a variable called region from X048WVS. I then wanted to create a state level variable when X048WVS is not equal to a region value. So I typed and received:

    gen str24 state = "."

    . des state

    storage display value
    variable name type format label variable label
    -------------------------------------------------------------------------------------------------------------------------------
    state str24 %24s

    . replace state = X048WVS if region1 == "."
    type mismatch
    r(109);

    where region is gen str24 state = "."

    . des state

    storage display value
    variable name type format label variable label
    -------------------------------------------------------------------------------------------------------------------------------
    state str24 %24s

    . replace state = X048WVS if region1 == "."
    type mismatch
    r(109);

    where region ==

    . fre X048WVS if S003 == 840

    X048WVS -- Region where the interview was conducted (WVS)
    ----------------------------------------------------------------------------------------
    | Freq. Percent Valid Cum.
    -------------------------------------------+--------------------------------------------
    Valid -2 No answer | 55 0.62 0.62 0.62
    840001 US: New England | 364 4.13 4.13 4.75
    840002 US: Middle Atlantic States | 962 10.91 10.91 15.66
    840003 US: South Atlantic | 971 11.01 11.01 26.67
    840004 US: East South Central | 462 5.24 5.24 31.91
    840005 US: West South Central | 670 7.60 7.60 39.51
    840006 US: East North Central | 996 11.29 11.29 50.80
    840007 US: West North Central | 418 4.74 4.74 55.54
    840008 US: Rocky Mountain state | 369 4.18 4.18 59.72
    840009 US: Northwest | 156 1.77 1.77 61.49
    840010 US: California | 445 5.05 5.05 66.54
    840011 US: Alaska | 3 0.03 0.03 66.57
    840012 US: Hawai | 4 0.05 0.05 66.62
    840013 US: Pacific | 348 3.95 3.95 70.56
    840201 US: AL Alabama | 28 0.32 0.32 70.88
    840202 US: AR Arkansas | 11 0.12 0.12 71.01
    840203 US: AZ Arizona | 73 0.83 0.83 71.83
    840204 US: CA California | 282 3.20 3.20 75.03
    840205 US: CO Colorado | 73 0.83 0.83 75.86
    840206 US: CT Connecticut | 26 0.29 0.29 76.15
    : | : : : :
    840231 US: NJ New Jersey | 56 0.63 0.63 89.36
    840232 US: NM New Mexico | 27 0.31 0.31 89.67
    840233 US: NV Nevada | 22 0.25 0.25 89.92
    840234 US: NY New York | 118 1.34 1.34 91.26
    840235 US: OH Ohio | 101 1.15 1.15 92.40
    840236 US: OK Oklahoma | 28 0.32 0.32 92.72
    840237 US: OR Oregon | 33 0.37 0.37 93.09
    840238 US: PA Pennsylvania | 83 0.94 0.94 94.04
    840239 US: RI Rhode Island | 8 0.09 0.09 94.13
    840240 US: SC South Carolina | 12 0.14 0.14 94.26
    840241 US: SD South Dakota | 24 0.27 0.27 94.53
    840242 US: TN Tennessee | 55 0.62 0.62 95.16
    840243 US: TX Texas | 177 2.01 2.01 97.17
    840244 US: UT Utah | 22 0.25 0.25 97.41
    840245 US: VA Virginia | 68 0.77 0.77 98.19
    840246 US: VT Vermont | 6 0.07 0.07 98.25
    840247 US: WA Washington | 65 0.74 0.74 98.99
    840248 US: WI Wisconsin | 68 0.77 0.77 99.76
    840249 US: WV West Virginia | 19 0.22 0.22 99.98
    840250 US: WY Wyoming | 2 0.02 0.02 100.00
    Total | 8819 100.00 100.00
    --------------------------------------------------------------------------------------

    and I wanted to assign state values of . fre X048WVS if S003 == 840

    X048WVS -- Region where the interview was conducted (WVS)
    ----------------------------------------------------------------------------------------
    | Freq. Percent Valid Cum.
    -------------------------------------------+--------------------------------------------
    Valid -2 No answer | 55 0.62 0.62 0.62
    840001 US: New England | 364 4.13 4.13 4.75
    840002 US: Middle Atlantic States | 962 10.91 10.91 15.66
    840003 US: South Atlantic | 971 11.01 11.01 26.67
    840004 US: East South Central | 462 5.24 5.24 31.91
    840005 US: West South Central | 670 7.60 7.60 39.51
    840006 US: East North Central | 996 11.29 11.29 50.80
    840007 US: West North Central | 418 4.74 4.74 55.54
    840008 US: Rocky Mountain state | 369 4.18 4.18 59.72
    840009 US: Northwest | 156 1.77 1.77 61.49
    840010 US: California | 445 5.05 5.05 66.54
    840011 US: Alaska | 3 0.03 0.03 66.57
    840012 US: Hawai | 4 0.05 0.05 66.62
    840013 US: Pacific | 348 3.95 3.95 70.56
    840201 US: AL Alabama | 28 0.32 0.32 70.88
    840202 US: AR Arkansas | 11 0.12 0.12 71.01
    840203 US: AZ Arizona | 73 0.83 0.83 71.83
    840204 US: CA California | 282 3.20 3.20 75.03
    840205 US: CO Colorado | 73 0.83 0.83 75.86
    840206 US: CT Connecticut | 26 0.29 0.29 76.15
    : | : : : :
    840231 US: NJ New Jersey | 56 0.63 0.63 89.36
    840232 US: NM New Mexico | 27 0.31 0.31 89.67
    840233 US: NV Nevada | 22 0.25 0.25 89.92
    840234 US: NY New York | 118 1.34 1.34 91.26
    840235 US: OH Ohio | 101 1.15 1.15 92.40
    840236 US: OK Oklahoma | 28 0.32 0.32 92.72
    840237 US: OR Oregon | 33 0.37 0.37 93.09
    840238 US: PA Pennsylvania | 83 0.94 0.94 94.04
    840239 US: RI Rhode Island | 8 0.09 0.09 94.13
    840240 US: SC South Carolina | 12 0.14 0.14 94.26
    840241 US: SD South Dakota | 24 0.27 0.27 94.53
    840242 US: TN Tennessee | 55 0.62 0.62 95.16
    840243 US: TX Texas | 177 2.01 2.01 97.17
    840244 US: UT Utah | 22 0.25 0.25 97.41
    840245 US: VA Virginia | 68 0.77 0.77 98.19
    840246 US: VT Vermont | 6 0.07 0.07 98.25
    840247 US: WA Washington | 65 0.74 0.74 98.99
    840248 US: WI Wisconsin | 68 0.77 0.77 99.76
    840249 US: WV West Virginia | 19 0.22 0.22 99.98
    840250 US: WY Wyoming | 2 0.02 0.02 100.00
    Total | 8819 100.00 100.00
    --------------------------------------------------------------------------------------

    and I wanted to attach values to state from W048WVS when W048WVS is a state, not a region. But my command didn;t work (see above). I tried to upload the data set (very small) but it wouldn;'t load. But this should be obvious to people who can do this better than I can.

    Thanks,

    Ric Uslaner

  • #2
    I tried to upload the data set (very small) but it wouldn;'t load
    Not upload, just use the command -dataex- (came with Stata 17, and can be installed using -ssc install dataex-). For example, you can type "dataex state region1, count(50)" to print 50 cases for state and region1, and the copy and paste that from the output onto here as code. (Emphasize again: not attachment, few people are brave enough to download files).

    If you provide data that way, it should increase the chance of the question being answered.

    Comment


    • #3
      Ken Chui gives excellent advice.

      It is hard for me to follow #1, For example, I can't grasp clearly what is meant by any of the mentions of the variable (variables?) region or region1. I understand the difference between states of the United States and broader regional divisions but the output from fre seems to imply variables with values that are variously states or regions.

      An attachment is promised but never delivered and in any case as Ken points out we greatly prefer a data example.

      fre is a community-contributed command from SSC: please note from https://www.statalist.org/forums/help#stata that you are asked to explain the provenance of such commands mentioned here.

      However, some suggestions are possible. As state is a string variable (shown by describe output) the failure of

      Code:
      replace state = X048WVS if region1 == "."
      with type mismatch implies clearly that X048WVS or region1 is really numeric. My guess is that X048WVS is numeric but with value labels. True or not,

      Code:
      gen wanted = X048WVS 
      may be a first stab at what Ric needs -- with whatever if condition is needed to select a subset. Naturally, some name other than wanted will seem sensible. Again, how region (region1) is pertinent here remains unclear. The difference is that as state was created as string, assigning a numeric variable to replace some of its values will fail, while creating a new variable will not.

      Note that there is no special meaning attached in Stata to string period ".". Missing string values are empty strings "" and conversely empty strings (and only empty strings) are treated as missing. Conversely assigning string period as a string value is entirely legal and might have some uses.

      https://www.stata.com/manuals/u12.pdf gives a good basic coverage of numeric variables, string variables and value labels which seems needed here. In a nutshell, type mismatch usually means offering a string (variable, scalar or constant) where numeric content is required by the context or offering numeric content (again a variable, scalar or constant) where string content is required, .

      Comment

      Working...
      X