Announcement

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

  • how do I generate multiple variables using foreach?

    Hi Stata Forum please help, I apologize if it may have been asked before in other posts
    I want to ask how to generate new variable using foreach, here the example of data :


    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input double points str5 duck double rank
    12471600 "duck1" 1
     5393360 "duck2" 2
     1574407 "duck3" 3
      696100 "duck4" 4
      629800 "duck5" 5
    end

    I want to generate data where variable points are divided by 1000000 & the new variable output looks like this:
    Click image for larger version

Name:	Screenshot 2024-07-29 100006.png
Views:	1
Size:	8.1 KB
ID:	1760057


    Please kindly your Advice,

    Thank you.



  • #2
    In your example output you don't seem to have divided by 1000000. Did you omit that by mistake, or did you not really want that? Anyway, here's code that does it, with dividing by 1000000. I think it's obvious how to modify the code if you didn't really want that division.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input double points str5 duck double rank
    12471600 "duck1" 1
     5393360 "duck2" 2
     1574407 "duck3" 3
      696100 "duck4" 4
      629800 "duck5" 5
    end
    
    assert rank == _n
    
    forvalues i = 1/`=_N' {
        gen duck`i'_point = points/1000000 if rank == `i'
    }
    drop points
    Note: Code assumes and verifies that rank runs consecutively from 1, with no gaps or ties, and that the observations are sorted in that order, as is the case in your example.

    Comment


    • #3
      Hi Clyde,
      Thank you so much for Your help. it's really work.
      Btw, how if I need to add specific formats in the code? example format %2.1 fc?

      Comment


      • #4
        On the main point consider also

        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input double points str5 duck double rank
        12471600 "duck1" 1
         5393360 "duck2" 2
         1574407 "duck3" 3
          696100 "duck4" 4
          629800 "duck5" 5
        end
        
        separate points, by(duck) gen(duck) veryshortlabel
        rename (duck?) (=_point)
        
        describe
        
        Contains data
         Observations:             5                  
            Variables:             8                  
        ----------------------------------------------------------------------------------------------------------
        Variable      Storage   Display    Value
            name         type    format    label      Variable label
        ----------------------------------------------------------------------------------------------------------
        points          double  %10.0g                
        duck            str5    %9s                  
        rank            double  %10.0g                
        duck1_point     long    %10.0g                duck1
        duck2_point     long    %10.0g                duck2
        duck3_point     long    %10.0g                duck3
        duck4_point     long    %10.0g                duck4
        duck5_point     long    %10.0g                duck5
        Otherwise if you want to divide by 1 million do that before calling separate

        Code:
        help format
        Last edited by Nick Cox; 29 Jul 2024, 03:41.

        Comment


        • #5
          Btw, how if I need to add specific formats in the code? example format %2.1 fc?
          After you have created the variables, apply the format you want.
          Code:
          format duck*_point %2.1fc
          N.B. No blank space in the format!

          Comment

          Working...
          X