Announcement

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

  • Loop with labmask

    Hi all!
    *Using STATA16
    I have a cross-sectional dataset with 100 observations and 736 variables. These variables are duplicated, some are strings, and others are numeric. For example, country_str (the string variable) and country_num (the numeric variable).

    I´ve been trying to label the values of the numeric variables by using the values of the string ones. For that, I saw the command lab mask. My problem is that I trying to make a loop by using locals and labsmask, but after some hours of trying, asking work colleagues, and looking on the internet I thought it might be easier to ask here.

    This is the code:


    Code:
    : 
    
    *1. Create a local containing only numeric variables
      
     ds *_num
     local numeric `r(varlist)'
     display "`numeric'"
    
    *2. Create a local containing only string variables
      
     ds *_str
     local string `r(varlist)'
     display "`string'"  
     
    *3. Label values of variables with a loop
      
     foreach x in $numeric {
            foreach y in $string {
             labmask `x', values (`y') 
            }    
            }


    Thank you very much!

  • #2
    Do all the variables have the same naming structure, i.e. var_str corresponds to var_num?

    Comment


    • #3
      Hi, yes!

      Comment


      • #4
        Try something like this? (warning: untested code!)

        Code:
        ds *_num
        local numeric `r(varlist)'
        
        local stubs: subinstr local numeric "_num" "", all
        
        foreach stub of local stubs {
            labmask `stub'_num, values(`stub'_str)
        }
        Edit: for those interested, labmask is a community-contributed command, available from the Stata Journal via
        Code:
        net describe gr0034, from(http://www.stata-journal.com/software/sj8-2)
        Last edited by Hemanshu Kumar; 15 Nov 2022, 08:16.

        Comment


        • #5
          Hi!
          Thank you very much! I am having some problems with the code. I got the message:

          Code:
          no observations
          r(2000);
          I have tried to make some changes by using (see code below), but I get the same error

          Code:
           ds, not(type string)

          Comment


          • #6
            Try this:
            Code:
            ds *_num
            local numeric `r(varlist)'
            
            local stubs: subinstr local numeric "_num" "", all
            
            foreach stub of local stubs {
                capture assert missing(`stub'_num)
                if !_rc {
                    dis as err "Variable `stub'_num has only missing values; skipping..."
                }
                    else labmask `stub'_num, values(`stub'_str)
            }

            Comment


            • #7
              Hi!!

              This last code worked, but I changed ...

              Code:
               
               ds *_num
              *...for ​​​​​​​
              Code:
                ds, has (type int byte)
              Thank you very much !!!!

              Comment

              Working...
              X