Announcement

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

  • accessing variable characteristics with Mata

    Hi, all. I'm working on my first piece of Mata code, which involves modifying the -codebookout- command so that it outputs variable characteristics as well as labels, etc. I'm having a rough time debugging the portion of the Mata script that accesses the variable characteristic (which I call "desc"), and I'm hoping someone can help.

    Code:
    clear mata
    mata:
    function mata_codebookout(){
    nv=st_nvar()
    A=J(0,6,"")
    for(i=1; i<=nv; i++ ){
     if(st_varvaluelabel(i)!=""){
       a1=st_varname(i),st_varlabel(i),st_vartype(i), st_local(i[desc])  
       st_vlload(st_varvaluelabel(i), values=., text=.)
       a2=(strofreal(values),text)
       a2_2="",""
       a2=a2_2\a2
       dum=length(a2[.,1])-1
       a1_2=J(dum,3,"")
       a1=a1\a1_2
       a=a1,a2
       A=A\a
      }
      else{
       b=st_varname(i),st_varlabel(i),st_vartype(i),"","Open ended"
       A=A\b
      }
     }
     return(A)
    }
    end
    The trouble spot is here:
    Code:
     a1=st_varname(i),st_varlabel(i),st_vartype(i), st_local(i[desc])
    I receive the following message:

    mata_codebookout(): 3200 conformability error
    <istmt>: - function returned error

    Thanks in advance for any help you can offer.

  • #2
    To get the variable characteristics from Mata, you should use st_global("varname[charname]") or st_global("_dta[charname]") function instead of st_local(), see below for an example:

    Code:
    . sysuse auto
    (1978 Automobile Data)
    
    . char define mpg[test] "this is a test"
    
    . char list
      _dta[note0]:                1
      _dta[note1]:                from Consumer Reports with permission
      mpg[test]:                  this is a test
    
    . mata:
    ------------------------------------------------- mata (type end to exit) ------
    : s = st_global("mpg[test]")
    
    : s
      this is a test
    
    : end
    --------------------------------------------------------------------------------
    Also, if you need work with characteristics that might be longer than 67,784 bytes, use Mata function st_lchar().




    Comment


    • #3
      Hi, Hua. Belated thanks for the reply! I am finally circling back to this issue, and am having some trouble sorting out the interpolation in Mata.


      Code:
      for(i=1; i<=nv; i++ ){
         a1=st_varname(i),st_global(st_varname(i)[desc]),st_vartype(i)  
      }
      From your example above, I want to reference st_global("mpg[test]"), but rather than calling mpg directly, I want to call it as one in an indexed list of variable names.

      Thanks for any help!.

      Comment


      • #4
        Try the following:

        Code:
        cscript
        
        sysuse auto, clear
        
        char define mpg[test] "this is a test for var [mpg]"
        char define foreign[test] "this is a test for var [foreign]"
        
        char list
        
        mata:
        
        var = st_nvar()
        for(i=1; i<=var; i++) {
                // construct char name using sprintf
            char_name = sprintf("%s[test]", st_varname(i))    
            s = st_global(char_name)
            if(s != "") {
                printf("%s\n", s)
            }
        }
        
        end
        The main point is to dynamically construct the name of the characteristics using sprint().

        Code:
        char_name = sprintf("%s[test]", st_varname(i))

        Comment

        Working...
        X