Announcement

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

  • Combine multiple variables into one-- stacking

    Click image for larger version

Name:	Capture.PNG
Views:	1
Size:	2.0 KB
ID:	1432120

    Hi all,
    I am trying to stack my data as follows (see above image)-- trying to create the 'd' variable. They are all string variables. Is there a way do this but still keep the rest of my dataset? Any help is greatly appreciated.
    Thanks!
    Nicole

  • #2
    The reshape long command will allow you to tranform your data into 3 observations for each 1 observation you have now, containing a single variable that has the values from your a, b, and c, and duplicating all other variables. Then you can drop the observations that you don't want to keep.

    I would give sample code but you don't give sample data to make that easier: Stata cannot import data from screen shots. To improve future posts, please review the Statalist FAQ linked to from the top of the page, as well as from the Advice on Posting link on the page you used to create your post. Note especially sections 9-12 on how to best pose your question.

    In particular, pictures are not a good way to present data or code or Stata output, even when they're posted at a readable size. The FAQ requests that you post sample data using the dataex command. If you are running Stata version 15.1, then dataex is part of your installation already. If running an earlier Stata, run ssc install dataex to get the command. Either way, run help dataex to read the simple instructions for using it. It takes just seconds to use dataex to provide a way for those who want to help you to create a complete and faithful replica of your example data in their own Stata. That, in turn, eliminates all sorts of questions that are left unanswered by descriptions or even by data listings and tables. And it enables people to test out code, so that you get the right answer the first time.

    The better you help others understand your problem, the better help others are able to give you in return.

    Comment


    • #3
      To add to William's excellent advice, another problem with picture is that they do not necessarily reproduce well on all computers. For example, on my setup, your picture came up smaller than a postage stamp and is completely unreadable. So do avoid pictures in the future.

      Comment


      • #4
        William & Clyde,
        Thanks for responding and my apologies for not being more clear.
        I followed your helpful instructions and used the dataex command to produce the problem I am dealing with.
        Essentially what I want to do is generate a third variable called in_accesscode with both access codes from in_paraccesscode and in_tnaccesscode in one column (included all as one variable (stack them on top of one another). Can you please let me know your thoughts on this?
        Many thanks!!
        Nicole


        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input str7(in_paraccesscode in_tnaccesscode)
        "1V4ZK92" ""       
        ""        "2K1LS92"
        "1Y4PK92" ""       
        ""        "2V5WH42"
        "1A2KE22" ""       
        ""        "2R7BJ22"
        "1B9VY52" ""       
        ""        "2D3GS92"
        "1R9AP72" ""       
        ""        "2Y1LD12"
        "1M4XG12" ""       
        ""        "2X2LD62"
        "1G4QH52" ""       
        ""        "2J5FP42"
        "1H9HD12" ""       
        ""        "2V9FQ72"
        "1M2HK42" ""       
        ""        "2C5PQ92"
        "1J3TU52" ""       
        ""        "2G1GT62"
        "1X2LQ42" ""       
        ""        "2H1LR32"
        "1L3US22" ""       
        ""        "2H9QX82"
        "1D8DR72" ""       
        ""        "2A3AA82"
        "1C6WD72" ""       
        ""        "2Y5MC22"
        "1F5PC92" ""       
        ""        "2Q4ML22"
        end
        ------------------ copy up to and including the previous line ------------------

        Comment


        • #5
          If I understand you correctly, this is nothing to do stack or reshape. It is just concatenation or string addition (adding an empty string makes no difference, whether before or after an non-empty string).

          But your full dataset may not be as well-behaved as your example. Here is one test:

          Code:
          gen nmissing = missing(in_paraccesscode) + missing(inaccesscode)
          edit if nmissing != 1
          missing() returns 1 if its argument is missing.

          Here's the optimistic solution using your example data

          Code:
          . gen wanted = in_paraccesscode + in_tnaccesscode
          
          . list
          
               +-------------------------------+
               | in_par~e   in_tna~e    wanted |
               |-------------------------------|
            1. |  1V4ZK92              1V4ZK92 |
            2. |             2K1LS92   2K1LS92 |
            3. |  1Y4PK92              1Y4PK92 |
            4. |             2V5WH42   2V5WH42 |
            5. |  1A2KE22              1A2KE22 |
               |-------------------------------|
            6. |             2R7BJ22   2R7BJ22 |
            7. |  1B9VY52              1B9VY52 |
            8. |             2D3GS92   2D3GS92 |
            9. |  1R9AP72              1R9AP72 |
           10. |             2Y1LD12   2Y1LD12 |
               |-------------------------------|
           11. |  1M4XG12              1M4XG12 |
           12. |             2X2LD62   2X2LD62 |
           13. |  1G4QH52              1G4QH52 |
           14. |             2J5FP42   2J5FP42 |
           15. |  1H9HD12              1H9HD12 |
               |-------------------------------|
           16. |             2V9FQ72   2V9FQ72 |
           17. |  1M2HK42              1M2HK42 |
           18. |             2C5PQ92   2C5PQ92 |
           19. |  1J3TU52              1J3TU52 |
           20. |             2G1GT62   2G1GT62 |
               |-------------------------------|
           21. |  1X2LQ42              1X2LQ42 |
           22. |             2H1LR32   2H1LR32 |
           23. |  1L3US22              1L3US22 |
           24. |             2H9QX82   2H9QX82 |
           25. |  1D8DR72              1D8DR72 |
               |-------------------------------|
           26. |             2A3AA82   2A3AA82 |
           27. |  1C6WD72              1C6WD72 |
           28. |             2Y5MC22   2Y5MC22 |
           29. |  1F5PC92              1F5PC92 |
           30. |             2Q4ML22   2Q4ML22 |
               +-------------------------------+

          Comment


          • #6
            Thanks Nick! That solution worked perfectly-- strangely I tried it before and it didn't work but I gave it a second go and success!

            Comment

            Working...
            X