Announcement

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

  • #16
    Originally posted by Romalpa Akzo View Post
    Also try below code:
    Code:
    gen Age = .
    
    forval i = 2(-1)0 {
    forval j = 4(-1)0 {
    capture replace Age = n_20007_`i'_`j' if n_20001_`i'_`j' == 1044
    }
    }
    Notice:

    1. You should modify the numbers 2, 4 (in red) with the actual numbers.

    2. With your actual data, the output of this code and Clyde's in #13 may be different. While Clyde's code concentrates on the appearance order of variables n_20001*, mine follows the sequential order of those variable names. Thus, you should be clear about what you do need in case those orders differing from each other.
    Wow, Thank you Romalpa. I wonder what you mean about those number in red? What it should be and what should I look for in the actual data to know it?

    Also, you are right about multiple age value appearance. How can take the lowest value if there are different age value for 1044?


    Thanks again!

    Comment


    • #17
      You can't add that condition. You can use that condition instead of the original condition.

      Code:
      ds n_20001* local n20001s `r(varlist)' gen Age = . foreach v of varlist `n20001s' { local pairing: subinstr local v "20001" "20007" confirm numeric var `pairing' replace Age = `pairing' if `v' == 1044 & `pairing' < Age }

      Comment


      • #18
        Originally posted by Clyde Schechter View Post
        You can't add that condition. You can use that condition instead of the original condition.

        Code:
        ds n_20001* local n20001s `r(varlist)' gen Age = . foreach v of varlist `n20001s' { local pairing: subinstr local v "20001" "20007" confirm numeric var `pairing' replace Age = `pairing' if `v' == 1044 & `pairing' < Age }
        This is it. It worked perfectly. I can't thank you enough Clyde! Thank you so much!

        Comment


        • #19
          Originally posted by Romalpa Akzo View Post
          Also try below code:
          Code:
          gen Age = .
          
          forval i = 2(-1)0 {
          forval j = 4(-1)0 {
          capture replace Age = n_20007_`i'_`j' if n_20001_`i'_`j' == 1044
          }
          }
          Notice:

          1. You should modify the numbers 2, 4 (in red) with the actual numbers.

          2. With your actual data, the output of this code and Clyde's in #13 may be different. While Clyde's code concentrates on the appearance order of variables n_20001*, mine follows the sequential order of those variable names. Thus, you should be clear about what you do need in case those orders differing from each other.
          Dear Romalpa Akzo now I understand what you meant about those number in red and now I can use the same code for string variables as well which I am grateful for (unlike Clyde code which is too advanced for me to tweak it). However, can you please tell me how I add one condition to it which is taking the lowest age values in case there are multiple occurrence?

          My code is like this:

          Code:
          gen Age_C1 =.
          
          forval i = 16(-1)0 {
          capture replace Age_C1 = n_40008_`i'_0 if s_40006_`i'_0 == "C61"
          }
          What if there are several C61 coded, how can I tweak the code to take the lowest age value?

          Thanks in advance!

          Comment


          • #20
            Indeed, Clyde has instructed you on the additional qualifier to get the lowest age value.

            A small modification of your code in #19 could be:
            Code:
            gen Age_C1 =.
             
            forval i = 16(-1)0 {
            capture replace Age_C1 = min(Age_C1, n_40008_`i'_0) if s_40006_`i'_0 == "C61"
            }

            Comment


            • #21
              I have tried Clyde code and applied it for string data, it worked but returned some missing values where it should'n't. Your code now is perfect. Thanks!

              Comment

              Working...
              X