Announcement

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

  • Creating a unique ID using 4 variables

    Hello Everyone,

    Can someone help me creat a unique ID by using 4 variables i.e. branch (2 categories), club (10 categories), ELA member (2 categories), and household (max 40 observations per club)

    The idea is to get ID system that combines branch, club, household, and ELA member, looking something like BCHHEM? For example, 104050 would be branch=1, club=4, household=5, and non ela member.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte branch float ELA byte club str41 id
    1 1 4 "uuid:029a6e89-1f8a-46f6-8390-0b6acf39aa8e"
    1 0 4 "uuid:ee5841d7-dda2-4bb6-87b8-31ea089fda3e"
    1 1 4 "uuid:95d5651e-8828-4432-861d-a41759203410"
    1 1 4 "uuid:4e3d25e8-94e6-4b7a-aa52-666c5fb07cef"
    1 1 5 "uuid:2f4a7196-8a2f-47a5-aab3-f58221e4525d"
    1 0 5 "uuid:13100224-60ed-4b18-8fdb-5dd176300427"
    1 0 5 "uuid:47da6e61-112b-487d-8224-cfe30c356c63"
    1 1 5 "uuid:83797e5b-9a82-4740-97bc-baf42ce2697a"
    2 1 8 "uuid:cafccc55-77b0-47dc-a951-08d527e3b6b6"
    2 1 8 "uuid:8ec49aa7-4b36-4053-8cd1-a17ea642f434"
    end
    label values branch branch
    label def branch 1 "Kasubi", modify
    label def branch 2 "Wakiso", modify
    label values ELA ela
    label def ela 0 "Non ELA Member", modify
    label def ela 1 "ELA Member", modify
    label values club club
    label def club 4 "Makerere", modify
    label def club 5 "Munaku", modify
    label def club 8 "Kikubapanga A", modify

  • #2
    danishussalam:
    try:
    Code:
    egen A=group( branch ELA club id)

    Kind regards,
    Carlo
    (Stata 18.0 SE)

    Comment


    • #3
      Originally posted by Carlo Lazzaro View Post
      danishussalam:
      try:

      Code:
      egen A=group( branch ELA club id)
      Hi Carlo,

      Thanks for your reply. Instead of giving me id in the format desired i.e. BCHHEM - For example, 104050 would be branch=1, club=4, household=5, and non ela member, the above code simply returns a single digit for every observation for instance 1 if club is 1 and branch is 1 etc, and 2 if club is 2 branch is 1 etc..

      Is there a way to get a code in such a way that it takes labels from the variables to make the ids? For instance, the code take 1 from branch 1, 3 from club 3 and 34 from respondent 34 and gives an id 010334?

      Thanks.

      Comment


      • #4
        You could just multiply the variables by a power of ten and sum. Or you could concatenate string conversions of the components. In general, identifiers are better off as strings. So:
        Code:
        generate str identifier = string(10 * branch) + string(10 * club) + string(household, "%02.0f") + string(ELA)
        1. Why are you multiplying branch and club by 10?
        2. household isn't in your data extract shown and so I could only guess at what range of values their numbers take on. Modify the display format in the code above as needed.
        3. I don't recommend making your identifiers from value labels; stick to making identifiers from your data.

        Comment


        • #5
          Note also the -label- option of -egen-'s -group ()- function.

          Comment


          • #6
            Originally posted by Joseph Coveney View Post
            You could just multiply the variables by a power of ten and sum. Or you could concatenate string conversions of the components. In general, identifiers are better off as strings. So:
            Code:
            generate str identifier = string(10 * branch) + string(10 * club) + string(household, "%02.0f") + string(ELA)
            1. Why are you multiplying branch and club by 10?
            2. household isn't in your data extract shown and so I could only guess at what range of values their numbers take on. Modify the display format in the code above as needed.
            3. I don't recommend making your identifiers from value labels; stick to making identifiers from your data.
            Hi Joseph,

            Thanks for your reply.

            I don't intend to multiply the branch and club by 10. The example 104050 is actually 1-04-05-0 - I only have two branches hence the one digit for branch. The two branches have five clubs each making a total of 10 clubs, hence two digits for branches, followed by the household members which can take a maximum of 40 and later ELA status (1 or 0). Every observation represents a household. You're right, the data doesn't have household.

            The plan was to create variable id using bysort club: gen household = _n

            This household variable will be used with branch, club and ELA to create a unique id that combines information from all four variables. I do plan to make the id variables from data rather than the value labels. Apologies for the confusion.

            I have modified the display format in the code.

            Comment


            • #7
              Modified code

              Code:
              * Example generated by -dataex-. To install: ssc install dataex
              clear
              input byte branch float ELA byte club str41 id
              1 1 4 "uuid:029a6e89-1f8a-46f6-8390-0b6acf39aa8e"
              1 0 4 "uuid:ee5841d7-dda2-4bb6-87b8-31ea089fda3e"
              1 1 4 "uuid:95d5651e-8828-4432-861d-a41759203410"
              1 1 4 "uuid:4e3d25e8-94e6-4b7a-aa52-666c5fb07cef"
              1 1 5 "uuid:2f4a7196-8a2f-47a5-aab3-f58221e4525d"
              1 0 5 "uuid:13100224-60ed-4b18-8fdb-5dd176300427"
              1 0 5 "uuid:47da6e61-112b-487d-8224-cfe30c356c63"
              1 1 5 "uuid:83797e5b-9a82-4740-97bc-baf42ce2697a"
              2 1 8 "uuid:cafccc55-77b0-47dc-a951-08d527e3b6b6"
              2 1 8 "uuid:8ec49aa7-4b36-4053-8cd1-a17ea642f434"
              2 1 8 "uuid:1c2e5f7f-8da5-48e0-8560-07ad12626938"
              1 0 4 "uuid:e024181b-d756-4e34-bbd0-1eea44359fe5"
              1 0 4 "uuid:94d604fb-5de9-42ea-bfbe-23c88e472b9d"
              1 0 4 "uuid:c647effe-bb03-4f90-a8a6-fa1e404d055e"
              2 . . "uuid:e6461b46-dbb0-4c48-a2aa-02842c2808ca"
              2 1 7 "uuid:93bb3fa0-5cfe-4361-8302-ea0aedccff5e"
              1 1 4 "uuid:4b3f06a1-affa-4c20-9876-80c44648b8f0"
              1 1 4 "uuid:fedee8e9-7fad-4fed-82ac-8299c8e026c9"
              1 0 4 "uuid:764bab63-2187-4437-b06c-095395139104"
              2 1 8 "uuid:6554a5fc-0d92-4338-8d4f-40f6452a4507"
              2 0 8 "uuid:36cb84cf-d5f3-4a1b-81ab-3c3e4e674346"
              2 0 8 "uuid:5ee33d37-b339-426b-ae03-80bb5ceb823d"
              2 0 6 "uuid:6899106f-78d6-4188-b55f-6649cca91af0"
              2 0 6 "uuid:67b3a161-fe5a-44a3-91a8-5271b5a1dacb"
              2 0 6 "uuid:c8b57c93-32f5-478a-a710-7276593d77f1"
              2 . . "uuid:ddd40027-8b70-4927-91fd-945e0370aafd"
              2 . . "uuid:d883ef38-b4d7-49b0-b743-396ef63287f5"
              2 0 6 "uuid:2197b357-b95c-4004-927a-a84375b00edd"
              2 . . "uuid:c562b589-b61c-42db-a6d6-57af52af77b3"
              2 . . "uuid:cf4148fa-5207-4c7b-860d-823410cf7359"
              2 . . "uuid:c744ac32-967f-47fb-b7d4-e66a0413030c"
              2 . . "uuid:df1bccd2-13a4-43a4-9ac5-e99f2588f37e"
              2 0 6 "uuid:5d32736b-9af8-453a-a8b4-f4b040f687f7"
              1 0 5 "uuid:58f2cf11-75df-4779-977f-b0df608a8f5e"
              1 0 5 "uuid:9680b5aa-edd1-490b-9be6-1bbd35ce7fed"
              1 0 5 "uuid:c2129646-fced-4d7b-91c0-67bec13da7f9"
              1 1 5 "uuid:2d4347ff-add9-485c-9075-252ff53c9a14"
              1 . . "uuid:5bd9d907-aa39-48f9-80a3-bcb7a3c16bc9"
              2 0 8 "uuid:d6a55b04-9299-4d1c-8eb0-1f6af6f46a51"
              2 1 8 "uuid:b2977a8d-5afe-4ff6-8578-a173d126ec1c"
              2 1 9 "uuid:a2ddc05f-dea4-4dce-ac59-f83725352c06"
              2 0 9 "uuid:20c4c9bc-5469-4f50-b217-a8901a280068"
              2 . . "uuid:25207953-cf40-419e-9f24-43f3584a8c57"
              2 0 9 "uuid:230266ee-7ddf-41e0-a651-64150537bac7"
              2 0 9 "uuid:d5c307f1-2c53-44a5-a17f-725c4a087ea4"
              2 . . "uuid:da9c449b-5146-4fbc-8528-30d2f3a4fc8a"
              2 . . "uuid:69e047fc-d6f0-47aa-87b5-f9fc80834819"
              2 1 9 "uuid:f2c95e08-c5cc-4caa-81cd-4fad02399851"
              2 0 8 "uuid:8ec10f02-44ec-4498-ac70-ad5766306b24"
              2 0 9 "uuid:b30e7532-ada8-4b87-a658-1a5acba01d5f"
              2 0 9 "uuid:ea9dddae-79c6-4c09-88e3-d994b3337e2f"
              2 . . "uuid:e6c26ed9-dd5f-41a8-9b57-6597bfc67537"
              2 1 9 "uuid:91f20a98-dadb-4bbc-8404-afa52474f694"
              2 1 9 "uuid:657cdc18-4838-412a-9c29-3d61d4b1295a"
              2 1 9 "uuid:c2feab25-007a-4e6c-8bf1-97821d8fdbcb"
              2 0 9 "uuid:2db2e677-eb59-4d73-9697-d21446285609"
              1 0 5 "uuid:330dd799-64c3-429b-b3b0-3b2ba0ce9612"
              1 . . "uuid:41701997-05bd-4959-96c3-f985f80344cb"
              1 0 5 "uuid:975b37f6-a1de-4ebd-87e7-a91d8aecb9cb"
              1 . . "uuid:dec874f9-3a33-4a7e-a84f-52a13579eb4d"
              1 0 5 "uuid:4cceeabc-639b-4c0d-a742-e57eabf2316b"
              1 . . "uuid:60953f64-fa9b-4b85-b6de-759cedb44663"
              1 0 3 "uuid:99b43958-505a-4ef7-9934-ffd1fda67768"
              2 1 9 "uuid:ea65facf-f12a-4e31-8a03-88758d6699a5"
              2 . . "uuid:08efd152-a864-4fdc-b5c8-983da08819ce"
              2 . . "uuid:60c8ec8c-36b3-4cec-ace4-623942ec1335"
              2 . . "uuid:c4a4ed0a-4ca3-4352-93c6-06348e69dc20"
              2 0 6 "uuid:cc7bf2ee-d57f-4ef1-bbfa-73d6ee742b81"
              2 0 6 "uuid:95f7472d-b829-4cb3-95dd-c87aaf8d8098"
              2 . . "uuid:03009bc7-f588-4654-9740-6b31c8137af0"
              2 1 6 "uuid:8428d4bf-192f-447f-82fe-fad5503abaa8"
              2 0 6 "uuid:716acb16-3913-40d2-9cfb-72acd4e6eae5"
              2 0 6 "uuid:d4157426-d431-4731-a868-93d3d672cd87"
              2 0 6 "uuid:2d2998e7-a867-4cbb-a1a1-723fccaac8ed"
              2 1 6 "uuid:13f6368e-f95a-432b-b249-755457b91047"
              1 . . "uuid:ecd65430-926f-478e-97ce-6d299d25f749"
              1 1 5 "uuid:fdedfce7-12c2-4afb-8a51-00be334fc441"
              1 0 5 "uuid:db5fb944-1a0e-449b-b769-30393251b13b"
              1 . . "uuid:fe6815af-35d9-44f9-a0b0-a0fdc8a3fe9c"
              1 0 5 "uuid:14300241-77a5-4362-b1d9-66fc43fe8eb5"
              1 0 5 "uuid:4ea8b58b-ef8c-4110-8f40-37d21fd07a01"
              1 0 5 "uuid:cb745697-1880-4020-bc67-e1c3784174d9"
              1 0 4 "uuid:7995af26-38ab-4cb4-8b69-376c52097fb7"
              1 0 4 "uuid:43f85f1c-1515-4656-87c6-2f5f5a4664af"
              1 0 4 "uuid:c283d6c7-38ef-400e-9145-cfd5a100ca84"
              1 0 4 "uuid:857921d9-3c7f-4336-9dc8-9eb0715b1d50"
              1 0 4 "uuid:80408dfd-0269-4c94-b0a6-4aea37d21f4b"
              1 0 4 "uuid:0faaba4a-f40c-4e01-817d-7e186ae9246e"
              1 0 4 "uuid:ab8d4590-c9a0-4cfe-86e9-51f416fa3140"
              1 0 4 "uuid:a00981af-4379-455a-838e-54d4336e2b67"
              1 1 4 "uuid:a602b1b2-f738-4ef6-8b8a-e14b3804f4a2"
              1 1 4 "uuid:2d2683d6-925e-48fc-ad4c-7b9fa17a0bbb"
              2 . . "uuid:389e7a77-d13f-46e1-99b3-ea07f3b7eada"
              2 1 9 "uuid:ca658800-22d5-4ea1-ab9e-1fd349c62dde"
              2 1 8 "uuid:0fae9058-b8b8-4b7e-bb42-41bcdcfb7939"
              2 1 8 "uuid:1d6c950e-e5b4-49f8-93ea-41aade76b198"
              2 1 8 "uuid:e8a0d97a-142b-415b-bfbd-0ef5e2ad888a"
              1 . . "uuid:bb9aa0a2-7289-44a4-a9ed-33d4569d404d"
              1 . . "uuid:0f5a4c0d-2fb3-420f-b6a6-8c31d96db55d"
              1 . . "uuid:2d8d0a89-5c95-4b43-8a9e-9233e72dac9d"
              end
              label values branch branch
              label def branch 1 "Kasubi", modify
              label def branch 2 "Wakiso", modify
              label values ELA ela
              label def ela 0 "Non ELA Member", modify
              label def ela 1 "ELA Member", modify
              label values club club
              label def club 3 "Lugala", modify
              label def club 4 "Makerere", modify
              label def club 5 "Munaku", modify
              label def club 6 "Kaliiti", modify
              label def club 7 "Kasangombe", modify
              label def club 8 "Kikubapanga A", modify
              label def club 9 "Kikubapanga B", modify

              Comment


              • #8
                A small variation in the code provided by Joseph works.

                Thanks Joseph and Nick!

                Comment

                Working...
                X