Announcement

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

  • How to realize data deformation operation

    How to perform the following data operations as follows:

    (1) Place the data scattered in multiple rows on one row

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str2(a b c d)
    "a"  ""   ""   ""  
    ""   "b"  ""   ""  
    ""   ""   "c"  ""  
    ""   ""   ""   "d" 
    "fd" ""   ""   ""  
    ""   "fe" ""   ""  
    ""   ""   "fe" ""  
    ""   ""   ""   "ef"
    "de" ""   ""   ""  
    ""   "ss" ""   ""  
    ""   ""   ""   ""  
    ""   ""   ""   "fw"
    "cc" ""   ""   ""  
    ""   "fd" ""   ""  
    ""   ""   "ef" ""  
    ""   ""   ""   "ge"
    end
    My goal is to transform the data into the following data

    Click image for larger version

Name:	the target data.png
Views:	1
Size:	2.4 KB
ID:	1707873



    (2) How to store the serial number of a specific value in a variable A, or how to store the value of another variable B corresponding to a specific value in a variable A for subsequent operations

    The data are the same as aboveL

    Click image for larger version

Name:	the number.png
Views:	1
Size:	5.7 KB
ID:	1707874


    We want to store the serial number of the variable A when it is not empty, that is, the value of the corresponding variable obs ( the value of the red in the figure ), in the temporary element for subsequent data operations.

    Looking forward to your reply, looking forward to your help, Thank you very much,

  • #2
    I believe the following does what you want:
    Code:
    gen obs_no = _n
    gen group_num = sum(!missing(a))
    collapse (firstnm) obs_no a b c d, by(group_num)

    Comment


    • #3
      Thank you very much ,It's very king of you

      Comment

      Working...
      X