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
Comment