Announcement

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

  • how to generate variables contaning distinct values of other varaibles?

    For example, accordging to expDomain1 to expDomain4, I want to generate var6, which includes the distinct values of expDomain 1 to expDomain 4.
    Thanks so much!
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str32 expDomain1 str103 expDomain2 str32 expDomain3 str39 expDomain4 str58 var6
    ""                           ""                           ""                                 ""  ""                                                          
    "1.电子信息"             "1.电子信息"             "1.电子信息"                   "0" "1.电子信息0"                                           
    "1.电子信息"             "1.电子信息"             "1.电子信息"                   "0" "1.电子信息0"                                           
    "1.电子信息"             "1.电子信息"             "1.电子信息"                   "0" "1.电子信息0"                                           
    "6.新能源与高效节能" "6.新能源与高效节能" "6.新能源与高效节能"       "0" "6.新能源与高效节能0"                               
    ""                           ""                           ""                                 ""  ""                                                          
    "1.电子信息"             "1.电子信息"             "5.资源环境与可持续发展" "0" "1.电子信息5.资源环境与可持续发展0"           
    "1.电子信息"             "1.电子信息"             "1.电子信息"                   "0" "1.电子信息0"                                           
    "1.电子信息"             "3.新材料"                "5.资源环境与可持续发展" "0" "1.电子信息3.新材料5.资源环境与可持续发展0"
    "1.电子信息"             "1.电子信息"             "1.电子信息"                   "0" "6.新能源与高效节能0"                               
    end

  • #2
    I'm sure that there are better ways, maybe involving regular expression functions, but here's one way. (Begin at the "Begin here" comment"). By the way, you have an error in one observation of your example (see below; pardon the alignment).

    .ÿ
    .ÿversionÿ17.0

    .ÿ
    .ÿclearÿ*

    .ÿ
    .ÿquietlyÿinputÿstr32ÿexpDomain1ÿstr103ÿexpDomain2ÿstr32ÿexpDomain3ÿstr39ÿexpDomain4ÿstr58ÿvar6

    .ÿ
    .ÿ*
    .ÿ*ÿBeginÿhere
    .ÿ*
    .ÿquietlyÿgenerateÿvar6bÿ=ÿ""

    .ÿ
    .ÿforeachÿvarÿofÿvarlistÿexpDomain*ÿ{
    ÿÿ2.ÿÿÿÿÿÿÿÿÿquietlyÿreplaceÿvar6bÿ=ÿvar6bÿ+ÿ`var'ÿifÿstrpos(var6b,ÿ`var')ÿ==ÿ0
    ÿÿ3.ÿ}

    .ÿ
    .ÿlistÿifÿvar6ÿ!=ÿvar6b,ÿnoobsÿabbreviate(20)ÿclean

    ÿÿÿÿexpDomain1ÿÿÿexpDomain2ÿÿÿexpDomain3ÿÿÿexpDomain4ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿvar6ÿÿÿÿÿÿÿÿÿvar6bÿÿ
    ÿÿÿÿ1.电子信息ÿÿÿ1.电子信息ÿÿÿ1.电子信息ÿÿÿÿÿÿÿÿÿÿÿÿ0ÿÿÿ6.新能源与高效节能0ÿÿÿ1.电子信息0ÿÿ

    .ÿ
    .ÿexit

    endÿofÿdo-file


    .

    Comment


    • #3
      Originally posted by Joseph Coveney View Post
      I'm sure that there are better ways, maybe involving regular expression functions, but here's one way. (Begin at the "Begin here" comment"). By the way, you have an error in one observation of your example (see below; pardon the alignment).

      .ÿ
      .ÿversionÿ17.0

      .ÿ
      .ÿclearÿ*

      .ÿ
      .ÿquietlyÿinputÿstr32ÿexpDomain1ÿstr103ÿexpDomain2ÿstr32ÿexpDomain3ÿstr39ÿexpDomain4ÿstr58ÿvar6

      .ÿ
      .ÿ*
      .ÿ*ÿBeginÿhere
      .ÿ*
      .ÿquietlyÿgenerateÿvar6bÿ=ÿ""

      .ÿ
      .ÿforeachÿvarÿofÿvarlistÿexpDomain*ÿ{
      ÿÿ2.ÿÿÿÿÿÿÿÿÿquietlyÿreplaceÿvar6bÿ=ÿvar6bÿ+ÿ`var'ÿifÿstrpos(var6b,ÿ`var')ÿ==ÿ0
      ÿÿ3.ÿ}

      .ÿ
      .ÿlistÿifÿvar6ÿ!=ÿvar6b,ÿnoobsÿabbreviate(20)ÿclean

      ÿÿÿÿexpDomain1ÿÿÿexpDomain2ÿÿÿexpDomain3ÿÿÿexpDomain4ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿvar6ÿÿÿÿÿÿÿÿÿvar6bÿÿ
      ÿÿÿÿ1.电子信息ÿÿÿ1.电子信息ÿÿÿ1.电子信息ÿÿÿÿÿÿÿÿÿÿÿÿ0ÿÿÿ6.新能源与高效节能0ÿÿÿ1.电子信息0ÿÿ

      .ÿ
      .ÿexit

      endÿofÿdo-file


      .
      Thanks for this clever tip!

      Comment

      Working...
      X