Announcement

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

  • How to resolve "type mismatch" error

    I'm trying to input, reshape, and clean national labor force data from a .txt file into Stata (I'm using Stata/SE 17.0). Some of the variables are string variables. I'm adapting someone else's code; and unfortunately, my coding skills aren't advanced enough to detect the source of the error, and make the necessary corrections. I'm pasting a shortened version of the do file below. The "type mismatch" error emerges after the following line (just below). I've also pasted an example of the data (in its condition when the error emerges). How can I resolve the type mismatch error? Thank you for any help you can provide.

    Code:
    *SA, basic aggregates;
    replace series=2 if seriesid=="LNS11000000";
    Code:
    capture log close nationalempsit_sample
    log using nationalempsit_sample.log, text replace name(nationalempsit_sample)
    
    #delimit;
    clear;
    set more off;
    version 17.0;
    
    /*  Reads in national unemployment rate and counts of LF, emp, unemp, nilf, etc.
        from household survey.
    
    Data downloaded from http://data.bls.gov/cgi-bin/srgate on October 18, 2022.
    Series list:
    LNS11000000
    LNS11300000
    LNS12000000
    LNS12300000
    LNS12500000
    */
    
    #delimit;
    clear;
    
    insheet using nationalempsit_sample.txt, clear names;
    
    forvalues y=1948/2022 {;
      rename jan`y' m`y'01;
      rename feb`y' m`y'02;
      rename mar`y' m`y'03;
      rename apr`y' m`y'04;
      rename may`y' m`y'05;
      rename jun`y' m`y'06;
      rename jul`y' m`y'07;
      rename aug`y' m`y'08;
      rename sep`y' m`y'09;
      rename oct`y' m`y'10;
      rename nov`y' m`y'11;
      rename dec`y' m`y'12;
      rename annual`y' m`y';
    };
    foreach v of varlist m* {;
      capture confirm string variable `v';
      if !_rc {;
        gen byte popcntrl_`v'=(strpos(`v', "(1)")>0);
        qui replace `v'=subinstr(`v', "(1)", "", .);
        qui destring `v', replace;
      };
      else {;
        gen byte popcntrl_`v'=0;
      };
    };
    
    tempfile lfstats1 lfstats2;
    save `lfstats1';
    
    reshape long m popcntrl_m, i(seriesid) j(date);
    gen periodicity="Y" if date<100000;
    replace periodicity="M" if periodicity=="";
    gen year=date if periodicity=="Y";
    replace year=floor(date/100) if periodicity=="M";
    *gen month=ym(year, date-100*year) if periodicity=="M";
    *format month %tm;
    gen month=date-100*year if periodicity=="M";
    drop date;
      
    *SA, basic aggregates;
    replace series=2 if seriesid=="LNS11000000";
    replace series=3 if seriesid=="LNS11300000";
    replace series=4 if seriesid=="LNS12000000";
    replace series=5 if seriesid=="LNS12300000";
    replace series=6 if seriesid=="LNS12500000";
    
    
    drop seriesid;
    reshape wide m popcntrl_m, i(periodicity year month) j(series);
    foreach v of varlist popcntrl_m* {;
      su `v', meanonly;
      if r(mean)==0 {;
        drop `v';
      };
    };
    
    rename m2  lf_s    ;
    rename m3  lfp_s   ;
    rename m4  emp_s   ;
    rename m5  epop_s  ;
    rename m6  empft_s ;
    
    rename popcntrl_m1 popcntrl;
    drop popcntrl_m*;
                
    label var lf_s "Labor force (SA)";            
    label var lfp_s "LF part rate (SA)";
    label var emp_s "Employment (SA)";
    label var epop_s "Emp-Pop ratio (SA)";
    label var empft_s "Employment - usu FT (SA)";
    
    label var popcntrl "Pop. cntrls affect pop, emp, LF";
    
    sort periodicity year month;
    
    foreach v of varlist lfp_n epop_n ur_n lfp_s epop_s ur_s us_* {;
      replace `v'=`v'/100;
    };
    compress;
    
    sort year month;
    save nationalempsit_sample.dta, replace;
    
    log close nationalempsit_sample;
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str52 seriesid double m byte popcntrl_m str1 periodicity float(year month)
    "1 : Data affected by changes in population controls." . 0 "Y" 1948  .
    "1 : Data affected by changes in population controls." . 0 "Y" 1949  .
    "1 : Data affected by changes in population controls." . 0 "Y" 1950  .
    "1 : Data affected by changes in population controls." . 0 "Y" 1951  .
    "1 : Data affected by changes in population controls." . 0 "Y" 1952  .
    "1 : Data affected by changes in population controls." . 0 "Y" 1953  .
    "1 : Data affected by changes in population controls." . 0 "Y" 1954  .
    "1 : Data affected by changes in population controls." . 0 "Y" 1955  .
    "1 : Data affected by changes in population controls." . 0 "Y" 1956  .
    "1 : Data affected by changes in population controls." . 0 "Y" 1957  .
    "1 : Data affected by changes in population controls." . 0 "Y" 1958  .
    "1 : Data affected by changes in population controls." . 0 "Y" 1959  .
    "1 : Data affected by changes in population controls." . 0 "Y" 1960  .
    "1 : Data affected by changes in population controls." . 0 "Y" 1961  .
    "1 : Data affected by changes in population controls." . 0 "Y" 1962  .
    "1 : Data affected by changes in population controls." . 0 "Y" 1963  .
    "1 : Data affected by changes in population controls." . 0 "Y" 1964  .
    "1 : Data affected by changes in population controls." . 0 "Y" 1965  .
    "1 : Data affected by changes in population controls." . 0 "Y" 1966  .
    "1 : Data affected by changes in population controls." . 0 "Y" 1967  .
    "1 : Data affected by changes in population controls." . 0 "Y" 1968  .
    "1 : Data affected by changes in population controls." . 0 "Y" 1969  .
    "1 : Data affected by changes in population controls." . 0 "Y" 1970  .
    "1 : Data affected by changes in population controls." . 0 "Y" 1971  .
    "1 : Data affected by changes in population controls." . 0 "Y" 1972  .
    "1 : Data affected by changes in population controls." . 0 "Y" 1973  .
    "1 : Data affected by changes in population controls." . 0 "Y" 1974  .
    "1 : Data affected by changes in population controls." . 0 "Y" 1975  .
    "1 : Data affected by changes in population controls." . 0 "Y" 1976  .
    "1 : Data affected by changes in population controls." . 0 "Y" 1977  .
    "1 : Data affected by changes in population controls." . 0 "Y" 1978  .
    "1 : Data affected by changes in population controls." . 0 "Y" 1979  .
    "1 : Data affected by changes in population controls." . 0 "Y" 1980  .
    "1 : Data affected by changes in population controls." . 0 "Y" 1981  .
    "1 : Data affected by changes in population controls." . 0 "Y" 1982  .
    "1 : Data affected by changes in population controls." . 0 "Y" 1983  .
    "1 : Data affected by changes in population controls." . 0 "Y" 1984  .
    "1 : Data affected by changes in population controls." . 0 "Y" 1985  .
    "1 : Data affected by changes in population controls." . 0 "Y" 1986  .
    "1 : Data affected by changes in population controls." . 0 "Y" 1987  .
    "1 : Data affected by changes in population controls." . 0 "Y" 1988  .
    "1 : Data affected by changes in population controls." . 0 "Y" 1989  .
    "1 : Data affected by changes in population controls." . 0 "Y" 1990  .
    "1 : Data affected by changes in population controls." . 0 "Y" 1991  .
    "1 : Data affected by changes in population controls." . 0 "Y" 1992  .
    "1 : Data affected by changes in population controls." . 0 "Y" 1993  .
    "1 : Data affected by changes in population controls." . 0 "Y" 1994  .
    "1 : Data affected by changes in population controls." . 0 "Y" 1995  .
    "1 : Data affected by changes in population controls." . 0 "Y" 1996  .
    "1 : Data affected by changes in population controls." . 0 "Y" 1997  .
    "1 : Data affected by changes in population controls." . 0 "Y" 1998  .
    "1 : Data affected by changes in population controls." . 0 "Y" 1999  .
    "1 : Data affected by changes in population controls." . 0 "Y" 2000  .
    "1 : Data affected by changes in population controls." . 0 "Y" 2001  .
    "1 : Data affected by changes in population controls." . 0 "Y" 2002  .
    "1 : Data affected by changes in population controls." . 0 "Y" 2003  .
    "1 : Data affected by changes in population controls." . 0 "Y" 2004  .
    "1 : Data affected by changes in population controls." . 0 "Y" 2005  .
    "1 : Data affected by changes in population controls." . 0 "Y" 2006  .
    "1 : Data affected by changes in population controls." . 0 "Y" 2007  .
    "1 : Data affected by changes in population controls." . 0 "Y" 2008  .
    "1 : Data affected by changes in population controls." . 0 "Y" 2009  .
    "1 : Data affected by changes in population controls." . 0 "Y" 2010  .
    "1 : Data affected by changes in population controls." . 0 "Y" 2011  .
    "1 : Data affected by changes in population controls." . 0 "Y" 2012  .
    "1 : Data affected by changes in population controls." . 0 "Y" 2013  .
    "1 : Data affected by changes in population controls." . 0 "Y" 2014  .
    "1 : Data affected by changes in population controls." . 0 "Y" 2015  .
    "1 : Data affected by changes in population controls." . 0 "Y" 2016  .
    "1 : Data affected by changes in population controls." . 0 "Y" 2017  .
    "1 : Data affected by changes in population controls." . 0 "Y" 2018  .
    "1 : Data affected by changes in population controls." . 0 "Y" 2019  .
    "1 : Data affected by changes in population controls." . 0 "Y" 2020  .
    "1 : Data affected by changes in population controls." . 0 "Y" 2021  .
    "1 : Data affected by changes in population controls." . 0 "Y" 2022  .
    "1 : Data affected by changes in population controls." . 0 "M" 1948  1
    "1 : Data affected by changes in population controls." . 0 "M" 1948  2
    "1 : Data affected by changes in population controls." . 0 "M" 1948  3
    "1 : Data affected by changes in population controls." . 0 "M" 1948  4
    "1 : Data affected by changes in population controls." . 0 "M" 1948  5
    "1 : Data affected by changes in population controls." . 0 "M" 1948  6
    "1 : Data affected by changes in population controls." . 0 "M" 1948  7
    "1 : Data affected by changes in population controls." . 0 "M" 1948  8
    "1 : Data affected by changes in population controls." . 0 "M" 1948  9
    "1 : Data affected by changes in population controls." . 0 "M" 1948 10
    "1 : Data affected by changes in population controls." . 0 "M" 1948 11
    "1 : Data affected by changes in population controls." . 0 "M" 1948 12
    "1 : Data affected by changes in population controls." . 0 "M" 1949  1
    "1 : Data affected by changes in population controls." . 0 "M" 1949  2
    "1 : Data affected by changes in population controls." . 0 "M" 1949  3
    "1 : Data affected by changes in population controls." . 0 "M" 1949  4
    "1 : Data affected by changes in population controls." . 0 "M" 1949  5
    "1 : Data affected by changes in population controls." . 0 "M" 1949  6
    "1 : Data affected by changes in population controls." . 0 "M" 1949  7
    "1 : Data affected by changes in population controls." . 0 "M" 1949  8
    "1 : Data affected by changes in population controls." . 0 "M" 1949  9
    "1 : Data affected by changes in population controls." . 0 "M" 1949 10
    "1 : Data affected by changes in population controls." . 0 "M" 1949 11
    "1 : Data affected by changes in population controls." . 0 "M" 1949 12
    "1 : Data affected by changes in population controls." . 0 "M" 1950  1
    end

  • #2
    Well, the code you show cannot be run with the data example you posted because the example does not contain all of the variables referred to in the code.

    Nevertheless, the source of the problem is clear from the code itself. Or rather, the code itself narrows it down to one of two possible problems.

    When the command -replace series=2 if seriesid=="LNS11000000"- is reached, Stata's parser expects to find a numericvariable named series (or something that begins with series) and a string variable seriesid (or something that begins with seriesid). Up to that point in the code, there is no variable named series, and the only variable whose name starts with series is seriesid, which is a string variable, not numeric. I cannot tell from the code whether in fact there is some other variable named series, perhaps as part of the input data. But I can tell that none was created by the code shown. So there are two possibilities:

    1. The variable series, or seriessomething (other than seriesid) exists in the input, but is a string variable.
    2. There is no variable starting with series other than seriesid.

    In the second case, the solution is to have a command that creates the variable series before you reach that point in the code. It is actually odd that in the code shown, the first value assigned to series is 2, not 1. I suspect that the original version of that code at a command like -gen series = 1 if seriesid == "whatever"- and that has somehow gotten lost. Restoring that line would solve the problem.

    In the first case it is harder to know what to do. That variable series (or seriessomething) has to be converted to numeric. But the appropriate way of doing that cannot be discerned from the information shown. If it does appear in the input data set, we would need to see it in the example data. The conversion probably would rely on either -encode- or -destring-, but those are very different and applying either where the other is needed will lead to disaster. So more specific guidance can only be forthcoming if a -dataex- that contains that variable is shown.

    Comment


    • #3
      My guess is that a line has been omitted that generated series in the first place. So the reference to series is being interpreted as an abbreviated reference to seriesid — hence as an illegal attempt to replace a string variable with numeric values.

      Comment


      • #4
        You're right, Nick Cox! :/ When I amended the do file, I deleted the line that generates SERIES. So the command I pasted should actually read:

        Code:
        *SA, basic aggregates;
        gen series=2 if seriesid=="LNS11000000";
        With this correction, the do file runs smoothly. In an ideal world, I would have caught this error before posting here. Any case, thank you both for your time and help.

        Comment

        Working...
        X