Announcement

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

  • Splitting one variable containing two variable data

    Hello
    I have a variable 'implementscheme' which contains the names of various agriculture implements and the subsidy schemes associated. I have to separate the variable into 'implement' and 'scheme'. There are 150 different implements and 5 different schemes. Hence, I am not able to split the variable using space.

    The command I am using is split implementscheme, p("State Plan" "BGREI" "NMOOP" "NFSM" "SMAM" "RKVY" "NFSM-oilseeds") .
    T
    he resulting variable I get only has the separated implement names. I also need a separate variable containing scheme names for those implements. Please help on that. Data eg. below: (In the data, scheme name is at last, say State Plan or NFSM. Implement name comes in the front.)

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str86 implementscheme
    "PumpSet State Plan"                           
    "PumpSet State Plan"                           
    "PumpSet NFSM"                                 
    "PumpSet State Plan"                                                   
    "Folding LDPE Irrigation Pipe State Plan"      
    "IrrigationPipe NFSM"                          
    "PumpSet State Plan"                           
    "Folding LDPE Irrigation Pipe State Plan"                 
    "Zerotillage/SeedCumFertilizerDrill State Plan"
    "Tractor State Plan"                                                                       
    "SprayersDusterPower NFSM"                     
    "Zerotillage/SeedCumFertilizerDrill State Plan"
    "SprayersDusterManual State Plan"              
    "Weeder State Plan"                             
    "PumpSet NFSM"                                                         
    "Zerotillage/SeedCumFertilizerDrill NFSM"      
    "PumpSet State Plan"                           
    "PumpSet NFSM"                                 
    "Folding LDPE Irrigation Pipe State Plan"      
    "PumpSet NFSM"                                    
    end

  • #2
    split is (was; I am the original author) intended for splitting strings where the parsing characters are just dispensable separators, which is not your case.

    This may help. By my count you have 3 = 5 - 2 different kinds of scheme not mentioned in the example, although the syntax you give seems to imply 7 not 5.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str86 implementscheme
    "PumpSet State Plan"                           
    "PumpSet State Plan"                           
    "PumpSet NFSM"                                 
    "PumpSet State Plan"                                                   
    "Folding LDPE Irrigation Pipe State Plan"      
    "IrrigationPipe NFSM"                          
    "PumpSet State Plan"                           
    "Folding LDPE Irrigation Pipe State Plan"                 
    "Zerotillage/SeedCumFertilizerDrill State Plan"
    "Tractor State Plan"                                                                       
    "SprayersDusterPower NFSM"                     
    "Zerotillage/SeedCumFertilizerDrill State Plan"
    "SprayersDusterManual State Plan"              
    "Weeder State Plan"                             
    "PumpSet NFSM"                                                         
    "Zerotillage/SeedCumFertilizerDrill NFSM"      
    "PumpSet State Plan"                           
    "PumpSet NFSM"                                 
    "Folding LDPE Irrigation Pipe State Plan"      
    "PumpSet NFSM"                                    
    end
    
    clonevar implement = implementscheme 
    
    gen scheme = "" 
    
    foreach s in "State Plan" "NFSM" { 
        replace implement = trim(subinstr(implement, "`s'", "", .)) 
        replace scheme = "`s'" if strpos(implementscheme, "`s'") 
    } 
    
    compress implement 
    table implement scheme 
    
    -----------------------------------------------------------
                                       |         scheme        
                             implement |       NFSM  State Plan
    -----------------------------------+-----------------------
          Folding LDPE Irrigation Pipe |                      3
                        IrrigationPipe |          1            
                               PumpSet |          4           5
                  SprayersDusterManual |                      1
                   SprayersDusterPower |          1            
                               Tractor |                      1
                                Weeder |                      1
    Zerotillage/SeedCumFertilizerDrill |          1           2
    -----------------------------------------------------------

    Comment


    • #3
      Nick

      This worked perfectly!

      Thank you
      Smriti

      Comment

      Working...
      X