Announcement

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

  • Merge two rows to one rows

    Hi, Sir,

    I want to merge two rows to one row because the data set has the variables name as two rows.
    (you can see Suvey and Date from below)

    By searching here I acknowledge that I should use 'concatenate' command, but I can only use this command when I merge some columns to one column.


    v1 v2 v3 v4 v5
    Survey State 6-Digit County Census Place
    Date Code ID Code Code


    Additional, I don't know the reason why variable's name is v1 not Survey(e.g. in the first variable).


    Thank you, Sir.

  • #2
    You mean like this?

    .ÿclearÿ*

    .ÿ
    .ÿinputÿstr15ÿ(v1ÿv2ÿv3ÿv4ÿv5)

    ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿv1ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿv2ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿv3ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿv4ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿv5
    ÿÿ1.ÿSurveyÿStateÿ6-DigitÿCountyÿ"CensusÿPlace"
    ÿÿ2.ÿDateÿCodeÿIDÿCodeÿCodeÿ""
    ÿÿ3.ÿend

    .ÿ
    .ÿforeachÿvarÿofÿvarlistÿv?ÿ{
    ÿÿ2.ÿÿÿÿÿÿÿÿÿquietlyÿreplaceÿ`var'ÿ=ÿ`var'ÿ+ÿ"ÿ"ÿ+ÿ`var'[2]ÿinÿ1
    ÿÿ3.ÿ}

    .ÿdropÿinÿ2
    (1ÿobservationÿdeleted)

    .ÿlist,ÿnoobs

    ÿÿ+-------------------------------------------------------------------------+
    ÿÿ|ÿÿÿÿÿÿÿÿÿÿv1ÿÿÿÿÿÿÿÿÿÿÿv2ÿÿÿÿÿÿÿÿÿÿÿv3ÿÿÿÿÿÿÿÿÿÿÿÿv4ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿv5ÿ|
    ÿÿ|-------------------------------------------------------------------------|
    ÿÿ|ÿSurveyÿDateÿÿÿStateÿCodeÿÿÿ6-DigitÿIDÿÿÿCountyÿCodeÿÿÿCensusÿPlaceÿCodeÿ|
    ÿÿ+-------------------------------------------------------------------------+

    .ÿ
    .ÿexit

    endÿofÿdo-file


    .

    Comment


    • #3
      I think I understand what you're after now. Try something like the following.
      Code:
      foreach var of varlist v? {
          quietly replace `var' = `var' + " " + `var'[2] in 1
          local variable_label = `var'[1]
          label variable `var' "`variable_label'"
      }
      drop in 1/2
      describe
      list in 1/5, noobs
      You cannot have spaces in your variable names in Stata, and so you'll need to rename your v1 to v5 manually to some useful short token, for example, survey_dt, state_code, something_id, county_code and place_code. You can always label the variable with something meaningful (see the output of -describe- when you run the code above).

      Comment


      • #4
        Thanks everybody.
        Now it is working.

        I hope everybody has a good weekend !!

        Comment

        Working...
        X