Announcement

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

  • Generating a variable based on other variable

    Dear Profs and colleagues,

    I am going to generate a variable that shows how many export varieties firms export.
    Firm ID: NPC_FIC
    Commodity Code: artigo
    Type of trade (Export =2, Import=1): fluxo



    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input double NPC_FIC str10 fluxo str24 artigo
    10006 "Exporta��o" "01012990"
    10006 "Exporta��o" "01012990"
    10006 "Exporta��o" "01012990"
    10006 "Exporta��o" "01012990"
    10006 "Exporta��o" "01012990"
    10006 "Exporta��o" "01012990"
    10006 "Exporta��o" "01012990"
    10006 "Exporta��o" "01012990"
    500000001 ""           ""        
    500000002 ""           ""        
    500000002 ""           ""        
    500000002 ""           ""        
    500000002 ""           ""        
    500000002 ""           ""        
    500000002 ""           ""        
    500000002 ""           ""        
    500000002 ""           ""        
    500000002 ""           ""        
    500000033 ""           ""        
    500000033 ""           ""        
    500000033 ""           ""        
    500000033 ""           ""        
    500000033 ""           ""        
    500000033 ""           ""        
    500000033 ""           ""        
    500000033 ""           ""        
    500000033 ""           ""        
    500000033 ""           ""        
    500000033 ""           ""        
    500000050 ""           ""        
    500000050 ""           ""        
    500000050 ""           ""        
    500000050 ""           ""        
    500000050 ""           ""        
    500000050 ""           ""        
    500000069 ""           ""        
    500000069 ""           ""        
    500000073 ""           ""        
    500000073 ""           ""        
    500000083 ""           ""        
    500000083 ""           ""        
    500000083 ""           ""        
    500000083 ""           ""        
    500000083 ""           ""        
    500000083 ""           ""        
    500000083 ""           ""        
    500000083 ""           ""        
    500000083 ""           ""        
    500000083 ""           ""        
    500000083 ""           ""        
    500000101 ""           ""        
    500000101 ""           ""        
    500000101 ""           ""        
    500000101 ""           ""        
    500000101 ""           ""        
    500000101 ""           ""        
    500000104 ""           ""        
    500000106 ""           ""        
    500000113 ""           ""        
    500000113 ""           ""        
    500000113 ""           ""        
    500000113 ""           ""        
    500000119 "Exporta��o" "07019090"
    500000119 "Exporta��o" "07019090"
    500000119 "Exporta��o" "07019090"
    500000119 "Exporta��o" "07019090"
    500000119 "Importa��o" "07019090"
    500000119 "Importa��o" "07019090"
    500000119 "Importa��o" "07019090"
    500000119 "Importa��o" "07019090"
    500000119 "Importa��o" "07019090"
    Starting with :

    Code:
    by NPC_FIC fluxo (artigo), sort: gen n_ex = sum(artigo != artigo[_n-1])
    by NPC_FIC fluxo (artigo): replace n_ex = n_ex[_N]
    Could you please help me to finish that?

    Cheers,
    Paris

  • #2
    Hey Paris,

    this could be version of code for you to consider. It provides the exact same results for your example.
    Code:
    unique
    is a command available from ssc.

    Code:
    unique artigo if artigo!="", by(NPC_FIC fluxo) gen(n_ex)
    bysort NPC_FIC fluxo (n_ex): replace n_ex = n_ex[1]
    PS: Note, that when providing data there needs to be an
    Code:
    end
    in the last line of your example.

    Best
    Sebastian

    Comment


    • #3
      It is fantastic, Thanks Sebastian, also for reminding me of END.
      Though still --n_ex --does not distinguish the numbers of export. It shows export + import
      After this stage, I need to create a variable to show the quantity of only export. Except --keep if fluxo==1 -- Do you know other way?

      Comment


      • #4
        Hey Paris,

        actually the variable captures the number seperate by import and export (since fluxo is included in by). In your current example one can not really see this. If you do the following after you input your example (random example) and run again you can see the difference.

        Code:
        replace artigo = "07019091" if _n ==69
        unique artigo if artigo!="", by(NPC_FIC fluxo) gen(n_ex)
        bysort NPC_FIC fluxo (n_ex): replace n_ex = n_ex[1]
        I hope I am not getting something wrong.

        Sebastian
        Last edited by Sebastian Schirner; 23 Apr 2023, 10:04.

        Comment


        • #5
          Originally posted by Sebastian Schirner View Post
          Hey Paris,

          actually the variable captures the number seperate by import and export (since fluxo is included in by). In your current example one can not really see this.
          You are right. It was labeled in that way indeed.
          It has been dedicated number 1 for export and 2 for import.


          Comment

          Working...
          X