Announcement

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

  • Loop for classification

    Hi all,

    I have a code set that I want to define in groups. The groups should be a set of geographical regions (South, West, North, East), thus I have generated South=0, West=0 etc.

    If X001 to X003 is present in code 1-6 I would like to replace South=1 . If X004 to X005 is present in code 1-6 I would like to replace West=1. If T004 to T005 is present in code 1-6 I would like to replace East=1 etc.

    Is there an easy way to do this with a loop?

    input byte id str4(code1 code2 code3 code4 code5 code6) 1 "X001" "X001""X003" "" 2 "" "X001" "X002" "X001" "" "" 3 "X004" "" "X001" "" "" "X004" 4 "" "" "" "X004" "X005" "X004" 5 "T001" "T004" "T005" "T004" "T004" "T002" end Thank you very much in advance!

  • #2
    Code:
    clear 
    input byte id str4(code1 code2 code3 code4 code5 code6) 
    1 "X001" "X001""X003" "" 
    2 "" "X001" "X002" "X001" "" "" 
    3 "X004" "" "X001" "" "" "X004" 
    4 "" "" "" "X004" "X005" "X004" 
    5 "T001" "T004" "T005" "T004" "T004" "T002" 
    end 
    
    foreach reg in South West East { 
        gen `reg' = 0 
    }
        
    forval j = 1/6 { 
        replace South = 1 if inlist(code`j', "X001", "X002", "X003")
        replace West = 1 if inlist(code`j', "X004", "X005")
        replace East = 1 if inlist(code`j', "T004", "T005")
    }
    
    list 
    
         +--------------------------------------------------------------------------+
         | id   code1   code2   code3   code4   code5   code6   South   West   East |
         |--------------------------------------------------------------------------|
      1. |  1    X001    X001    X003                               1      0      0 |
      2. |  2            X001    X002    X001                       1      0      0 |
      3. |  3    X004            X001                    X004       1      1      0 |
      4. |  4                            X004    X005    X004       0      1      0 |
      5. |  5    T001    T004    T005    T004    T004    T002       0      0      1 |
         +--------------------------------------------------------------------------+

    Comment


    • #3
      Thank you Nick for your quick reply! The code interval want to define is in clusters of hundered X & T codes. Is there a way to define an 000-100 interval i.e. X000-100 or do I have to write down each "X001", "X002" etc?

      Comment


      • #4
        Problem right now is that I get Syntax "expression too long" if I specify each code.

        Comment


        • #5
          If you ask the wrong question, you may get a wrong answer!

          Yes, you could use inrange() rather than inlist() to the extent that codes are sequential.

          Comment


          • #6
            You are absolutely right Nick! I'm sorry for the inconvenience. With the inrange it works, thank you!

            Comment

            Working...
            X