Announcement

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

  • looping isuues

    Hi Everyone,

    I have a dataset something like:

    A_count B_count S_count.......Z_count A_percentage B_percentage S_percentage............Z_percentage
    ------------------------------------------------------------------------------------------------------------------------------------------------------------
    x x x x (xx.xx%) (xx.xx%) (xx.xx%) (xx.xx%)

    and i want to do some thing like

    finalA finalB finalS ...........finalZ
    ------------------------------------------------------------------------------------------------------------------------------------------------------------
    x(xx.xx%) x(xx.xx%) x(xx.xx%) x (xx.xx%)

    I'm trying to do this using foreach loop where i have a problem while joining the x[which is the count] with (xx.xx%)[percentages] which looks like concatenation but i have so many categories like A,C,S....Z that making it very cumbersome also to use a order function to get the final A, final B....final Z as i require in my output.

    If, any suggestions how it can be handled, please do share your comments ? Not asking particularly to write a code for me, but even if you share ideas to solve which can help me to take this to a final level, That would be sufficient .Hope I'm clear. Your comments would be a great learning for me guys!
    Last edited by Sitaram Sahoo; 29 Aug 2019, 07:27.

  • #2
    I find your question distinctly difficult to understand: First and foremost is that you have not told us what the relationship is of your final* variables are to your other variables, and you have not shown us enough about what the current structure of your dataset is, and how you want it to be after it is operated on. I'd encourage you to try a couple of things: 1) Take another look at the FAQ about how to effectively ask questions, and take a look in particular on how to use the -dataex- command to post example data; 2) Have a friend or colleague --not necessarily one experienced with Stata--look over your question to help you make it clear.

    Comment


    • #3
      Maybe you mean something like this?
      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input byte(A_count B_count C_count) float(A_percentage B_percentage C_percentage)
      1 3 5 11.11 33.33 55.56
      end
      
      foreach letter in `c(ALPHA)'{
          if "`letter'" <= "C"{
              gen final`letter' = string(`letter'_count) + "(" + string(`letter'_percentage) + "%)"
              drop `letter'_count `letter'_percentage
              }
          }
      list, noobs
      Code:
      . list, noobs
      
        +-----------------------------------+
        |    finalA      finalB      finalC |
        |-----------------------------------|
        | 1(11.11%)   3(33.33%)   5(55.56%) |
        +-----------------------------------+

      Comment


      • #4
        Hi Prof. Mike Lacy

        Glad to see that you looked my query. I have a dataset from a clinical trial. kindly, have a look at the sample table. It talks about the adverse events after administering a drug to a patient. Eg: No of times fever as an adverse event has recorded at a trial site(eg: Seatle hospital). Like this, I have a larger dataset which I will be using to produce a report on incidents of all types of symptoms recorded by hospitals with the counts and respective percentages.

        Currently, the dataset i have that can be considered as the input dataset looks like:
        No_Fever No_Diarrhea No_swollenthroat Feverpercent Diarrheapercent swollenthroatpercent
        12 11 15 (21.23)% (20.29%) (30.89%)


        I want the results to be reported out in this format as specified below.
        Fever Diarrhea Swollenthroat
        12(21.23%) 11(20.29%) 15(30.89%)
        Pls, let me know if the query looks clear to you now.

        Comment


        • #5
          My best advice for clarity involved using -dataex- as described in the FAQ. I see that your new posting doesn't follow that advice, nor have you explained why you can't. That reduces my interest in helping you. Perhaps someone else feels differently.

          Comment


          • #6
            How about

            Code:
              forv c=65/90 {
                   local L=char(`c')
                   gen str final`L'=string(`L'_count)+`L'_percentage
             }
            ?

            hth,
            Jeph

            Comment


            • #7
              Hi Jeph Herrin ,

              Thank you so much for the sample code. It worked well for me.

              With regards,
              Sitaram.

              Comment


              • #8
                Hi Prof. Mike Lacy,

                Thank you for your advice. I've gone through -dataex described in FAQ which i should have looked before posting which I'll keep in mind before posting in future. Sorry for my oversight to your reply.

                Regards,
                Sitaram

                Comment


                • #9
                  Originally posted by Wouter Wakker View Post
                  Maybe you mean something like this?
                  Code:
                  * Example generated by -dataex-. To install: ssc install dataex
                  clear
                  input byte(A_count B_count C_count) float(A_percentage B_percentage C_percentage)
                  1 3 5 11.11 33.33 55.56
                  end
                  
                  foreach letter in `c(ALPHA)'{
                  if "`letter'" <= "C"{
                  gen final`letter' = string(`letter'_count) + "(" + string(`letter'_percentage) + "%)"
                  drop `letter'_count `letter'_percentage
                  }
                  }
                  list, noobs
                  Code:
                  . list, noobs
                  
                  +-----------------------------------+
                  | finalA finalB finalC |
                  |-----------------------------------|
                  | 1(11.11%) 3(33.33%) 5(55.56%) |
                  +-----------------------------------+
                  Hi Wouter Wakker ,

                  I'm very new to Stata, so as to Stata macros.But, putting my best to learn. I would grateful if you can answer to following query.

                  1. What excatly `c(ALPHA)' is? how is it storing all the columns ?
                  2. How is "C " working when we have not stored anything neither defined it before?

                  Are these global macros?if yes, then how shouldn't they have to be defined? My apology if my question sounds very general, since I'm used to code in SAS,R so, It's taking little time for me to look in a different spec. So, kindly direct me towards finding the right answers.

                  Hope to receive your response soon!


                  Thanks,
                  Sitaram

                  Comment


                  • #10
                    1. What excatly `c(ALPHA)' is? how is it storing all the columns ?
                    2. How is "C " working when we have not stored anything neither defined it before?
                    -c()- refers to a set of built-in constants that are available to programmers in Stata. c(ALPHA) specifically is the alphabet in upper case, separated by blank spaces. Similarly c(alpha) gives the alphabet in lower case, separated by blank spaces. There are many different constants stored in -c()- and all of them can be accessed in Stata code by referencing them within local macro quotes. Some refer to important numbers or strings , some refer to aspects of your computer or your Stata configuration. To get a listing of all the constants in -c()- run -creturn list-. And for a full explanation of what each one is, see the section of the [P] manual on -creturn-. (Easiest way to access that is run -help creturn- and then click on the blue link to the manual at the top of the page.) -c()- is automatically initialized when you launch Stata, and some of its contents are automatically updated by Stata after certain commands. There is no need to store anything in it; in fact, it is not even possible to do so. -c()- is read-only.

                    Are these global macros?if yes, then how shouldn't they have to be defined?
                    No, they are not Stata macros at all. The contents of -c()- are accessed as if they are local macros, by using `c(whatever)'. But unlike local macros, these are available globally. And unlike true local or global macros, you cannot modify them.

                    I'm used to code in SAS,R so, It's taking little time for me to look in a different spec.
                    Yes, Stata's syntax and data model are very different from SAS, perhaps somewhat less different from R. In any case, it takes some getting used to. Having myself once made the transition from SAS to Stata, I can tell you that your background in SAS probably hurts more than it helps. Do your best to block any SAS-like thoughts when working with Stata: mostly they will be irrelevant, and where they appear relevant they will typically lead you astray.

                    Comment


                    • #11
                      Hi statlist fam,

                      am working on a filed survey data that has some of the questions requiring multiple responses. i would like to tally the number of times each response was mentioned, which will definately not tally with number of respondents because a response may be chosen more than once. Kindly help. A typical variable appears as such;

                      dataex AJ

                      ----------------------- copy starting from the next line -----------------------
                      Code:
                      * Example generated by -dataex-. To install: ssc install dataex
                      clear
                      input str14 AJ
                      "88"       
                      "6"        
                      "1 3"      
                      "1 2"      
                      "1"        
                      "1 2 3 4 5"
                      "1 2"      
                      "2 3"      
                      "1 2"      
                      "3 6"      
                      "1"        
                      "88"       
                      "88"       
                      "99"       
                      "1"        
                      "1 2"      
                      "2"        
                      "2 4"      
                      "1 3"      
                      "1 2 5"    
                      "1 2 3 4 5"
                      "2 3 4"    
                      "1 3 5"    
                      "1 2 3"    
                      "88"       
                      "1"        
                      "1"        
                      "1 2 3 4"  
                      "88"       
                      "1 2 3 4"  
                      "1"        
                      "2 3"      
                      "1"        
                      "1"        
                      "1 3"      
                      "88"       
                      "1 6"      
                      "2"        
                      "1 6"      
                      "88"       
                      "1 2"      
                      "4 6"      
                      "1 2"      
                      "88"       
                      "1 4"      
                      "1 2 4"    
                      "2"        
                      "1"        
                      "1"        
                      "1"        
                      "1 2"      
                      "1 2"      
                      "2 4"      
                      "1"        
                      "2 6 99"   
                      "1 6"      
                      "1 3"      
                      "99"       
                      "88"       
                      "1"        
                      "3"        
                      "1 2 99"   
                      "2 4"      
                      "3 4"      
                      "88"       
                      "1 2 3 5 6"
                      "1 3"      
                      "1"        
                      "1 2"      
                      "1 6"      
                      "1 6"      
                      "3 4"      
                      "1 2"      
                      "1"        
                      "88"       
                      "1 6"      
                      "1 6"      
                      "1 2 3 4"  
                      "1 2"      
                      "1 6"      
                      "1 2"      
                      "1"        
                      "1"        
                      "1 2 3"    
                      "1 4"      
                      "1 2 3 5"  
                      "1 2"      
                      "88"       
                      "99"       
                      "1 2 3"    
                      "1"        
                      "1 5"      
                      "1 2"      
                      "1 2 4"    
                      "1 2 6"    
                      "88"       
                      "1 3 4"    
                      "1"        
                      "1 2 3 4"  
                      "88"       
                      end

                      Comment


                      • #12
                        See
                        Code:
                        help split
                        Edit: Sorry. That won't work for your problem. I responded too quickly.

                        Red Owl
                        Stata/IC 16.0 (Windows 10, 64-bit)
                        Last edited by Red Owl; 11 Sep 2019, 08:51.

                        Comment


                        • #13
                          Code:
                          search tabsplit
                          typed in Stata
                          should yield a link to install package tab_chi from SSC.

                          Also search this forum for mentions of tabsplit.

                          Comment


                          • #14
                            Hi Nick
                            Thank you for the suggestion. i would like to confirm the command though to use the SSC
                            is it tabchi var?

                            Comment


                            • #15
                              No. Your first post has been deleted — why was that done? — but my recollection of your problem is that it.calls for tabsplit.

                              Comment

                              Working...
                              X