Dear Stata experts,
I downloaded Excel file. In some rows where is no data there is "NA". Here is my data example:
My idea was to replace "NA" with "." and then destring. Due to the fact that I have 790 variables it is time consuming and error yielding to tell stata which columns it should take into account. I tried it with a loop:
but got an error message r(111) variables out of order.
I checked it once again but didn't find my error
Is it possible to write a loop for all variables and stata would change it only for string variables so that it wouldn't be type mismatch. I tried this command:
Or is there any other way to destring my variables without replacing "NA" ?
I downloaded Excel file. In some rows where is no data there is "NA". Here is my data example:
Code:
* Example generated by -dataex-. For more info, type help dataex clear input str4 period double(ExportsfromAustraliatoAustri C D) str17 E double(F G) str17 H double I str2 J "1967" 1.68818291542382 .447193126169174 .254802440348146 "NA" .192390685821028 1.24098978925465 "1.23267336787171" .00831642138294219 "NA" "1968" 1.37783875343941 .255840493746482 .0766597683580703 "NA" .179180725388411 1.12199825969293 "1.08881040737231" .0331878523206176 "NA" "1969" 2.14021372461828 .328313422978159 .1290259660822 ".0011185009753137" .198168955920645 1.81190030164012 "1.71011616406372" .101784137576401 "NA" "1970" 1.37254388532754 .192041412896288 .103648950063559 ".0014473268885849" .0869451359441442 1.18050247243126 "1.09869892846481" .0818035439664519 "NA" "1971" 1.07272091518585 .376749961389412 .150304925135887 ".0043821532603898" .222062882993135 .695970953796438 ".635863227046547" .060107726749892 "NA" "1972" 1.03049032659761 .170154750132609 .0467655443634626 ".0040929481760718" .119296257593075 .860335576464996 ".6237951254248409" .236540451040155 "NA" "1973" 1.10660073292803 .554202972554974 .042445121273776 ".0147793837375182" .49697846754368 .552397760373058 ".0573032191211405" .495094541251917 "NA" "1974" .619402162186426 .110295313069868 .00421017570778895 ".0033361329234095" .10274900443867 .509106849116558 ".279385949993835" .229720899122722 "NA" "1975" .862150009789141 .457623355172952 .173436807927162 ".005716531769577" .278470015476213 .40452665461619 ".185512721799737" .219013932816453 "NA" "1976" 1.17399480567722 .18227499023391 .0899267099507063 ".0074502711364507" .084898009146753 .991719815443311 ".0138014496636041" .977918365779707 "NA" "1977" 1.0596683294864 .252186446517037 .0971412808940711 ".0175952383069712" .137449927315995 .807481882969366 "NA" .807481882969366 "NA" "1978" 2.74407206961562 .326298100332131 .226576840094671 ".0082933268536468" .0914279333838132 2.41777396928349 "1.78836960318622" .62940436609727 "NA" "1979" 2.43123089502343 .356706683016324 .236905414894377 ".0301890470330195" .0896122210889276 2.07452421200711 "1.8339683632572" .240555848749909 "NA" "1980" 3.73198211321387 .446112989371622 .141884411694923 ".023741354288482" .280487223388216 3.28586912384225 "2.66611877600431" .619750347837939 "NA" "1981" 2.86560192580854 .11650479750537 .00505486422107316 ".0256636617808451" .0857862715034518 2.74909712830317 "2.15416632696175" .594930801341411 "NA" "1982" 2.46898937574441 .0681212756305846 .0409741067841663 ".0229692068236075" .0041779620228108 2.40086810011382 "1.72690909535871" .673959004755113 "NA" "1983" 1.85716024628491 .171121272324057 .130544004006761 ".0077711427433588" .0328061255739369 1.68603897396085 "1.19175684073928" .49428213322157 "NA" "1984" 1.89950961141022 .55082007656801 .395247194341352 "NA" .155572882226658 1.34868953484221 ".881702543621684" .466986991220526 "NA" "1985" 6.74099973156501 4.51314312672418 4.32899960820237 ".0331840324351902" .150959486086617 2.22785660484083 "1.61705380051119" .610802804329642 "NA" "1986" 2.6416032194799 .922339003082142 .765309045323289 ".0230991871640027" .13393077059485 1.71926421639776 "1.1863779696994" .532886246698352 "NA" "1987" 3.50108724040966 1.00879296806758 .666709174063678 ".0338925172702406" .308191276733661 2.49229427234208 "1.41205142749612" 1.08024284484596 "NA" "1988" 5.89184415767255 4.87417369147086 4.32603952670905 ".0309162835204018" .517217881241407 1.01767046620168 ".210563597502116" .807106868699568 "NA" "1989" 2.10293175939197 1.11200655142119 .945336338903254 ".0364085343299939" .130261678187945 .990925207970772 ".0316882212792983" .959236986691474 "NA" "1990" 3.09566901071224 .57895066182819 .294643535250431 ".0593818650269951" .224925261550764 2.51671834888405 ".0291394062259312" 2.48757894265812 "NA" "1991" 4.5952602023237 2.25751780793534 2.11941353857843 ".0506122384234352" .0874920309334809 2.33774239438835 ".0126623620892795" 2.32508003229908 "NA" "1992" 5.2282254216153 3.62389907526694 3.2407176217882 ".156218000188867" .226963453289869 1.60432634634836 ".129686962949035" 1.47463938339932 "NA" "1993" 3.60528197241182 2.27059248575925 2.15936564523635 ".0440518674912489" .0671749730316509 1.33468948665257 ".0175523209796626" 1.31713716567291 "NA" "1994" 4.20129878529848 2.61912210318222 2.48602899558369 ".0897388701919393" .0433542374065943 1.58217668211626 ".0396183580856388" 1.54255832403062 "NA" "1995" 1.66267308728776 .718284669540413 .601226761357698 ".0763861976604632" .0406717105222516 .944388417747347 "NA" .944388417747347 "NA" "1996" 1.67174006197263 .782547191550645 .713766766181845 ".0226147164761684" .0461657088926319 .889192870421989 ".0068648019715908" .882328068450398 "NA" "1997" 1.46324874669685 1.14965136733421 1.02237855424437 ".0162570073507562" .111015805739078 .313597379362646 "NA" .313597379362646 "NA" "1998" 1.49118616589432 1.33347393623139 1.17672043293244 ".126269767485836" .0304837358131181 .157712229662932 ".0037474515003959" .153964778162536 "NA" "1999" .797078970751392 .71046188236338 .620047433227721 ".0772284463703214" .0131860027653374 .0866170883880116 "NA" .0866170883880116 "NA" "2000" .685350935246251 .614281599995358 .548381828240676 ".0590893523680979" .00681041938658401 .0710693352508932 ".0022994950223443" .0687698402285489 "NA" "2001" .790855012105365 .575420760492461 .517316716681913 ".0434616679209898" .0146423758895586 .215434251612903 ".0032128958372724" .212221355775631 "NA" "2002" .678040814956461 .572556585585948 .481116637902978 ".0533440622037423" .0380958854792272 .105484229370513 "NA" .105484229370513 "NA" "2003" .962200827340452 .648574348691287 .387374677722107 ".174542912918566" .0866567580506144 .313626478649164 "NA" .313626478649164 "NA" "2004" .539462037877982 .46406333019581 .289660600577625 ".0960438231260797" .078358906492105 .0753987076821722 "NA" .0753987076821722 "NA" "2005" .829157935583996 .501237842646939 .0739293580848232 ".198353245480259" .228955239081856 .327920092937058 ".0044760758878302" .323444017049227 "NA" "2006" .892993578681696 .297647331955208 .0716496026031399 ".154609313711333" .0713884156407351 .595346246726488 "NA" .595346246726488 "NA" "2007" 1.1831879757206 .330713317689642 .0790955954086102 ".188937995410851" .0626797268701809 .852474658030954 "NA" .852474658030954 "NA" "2008" .722686752070487 .254740682283988 .0355505518170268 ".0736625387465388" .145527591720423 .467946069786498 ".0681353266478771" .399810743138621 "NA" "2009" 1.25267905943902 .785066710532028 .0507452481777911 ".529209776576246" .205111685777991 .467612348906992 "NA" .467612348906992 "NA" "2010" 1.33814239274765 .687448088730707 .101268940320203 ".211859216032929" .374319932377575 .650694304016942 "NA" .650694304016942 "NA" "2011" 1.89234563458849 .981591215438803 .263935645697609 ".38498013940013" .332675430341064 .910754419149686 "NA" .910754419149686 "NA" "2012" 1.4661200700721 .764122338527107 .194755663981232 ".135434932164803" .433931742381071 .701997731544994 ".0093917801338615" .692605951411133 "NA" "2013" 1.73105339685472 1.04842663639813 .330930068599616 ".0609117081286965" .656584859669814 .68262676045659 "NA" .68262676045659 "NA" "2014" 1.25397735481203 .851379333036416 .147158715256268 ".108021829853592" .596198787926557 .402598021775617 "NA" .402598021775617 "NA" "2015" 1.75072124689188 1.39002801113872 .454976523638499 ".0571724831827264" .877879004317496 .360693235753163 ".0010879831275599" .359605252625603 "NA" "2016" 1.00536622930604 .584672332418586 .149590925095184 ".195573416796196" .239507990527205 .420693896887459 ".0013535368543057" .419340360033154 "NA" end
My idea was to replace "NA" with "." and then destring. Due to the fact that I have 790 variables it is time consuming and error yielding to tell stata which columns it should take into account. I tried it with a loop:
Code:
foreach x of varlist J-AD AF-AG AJ-AP AT AV-BP BT-CX CZ-DN DQ-EA EC-EE EH-EW EY-FB FD FH-FI FK-FM FO-GA GC-GD GG-GU GW-HD HF-HI HL-HW HY-HZ IB-IC IG-JC JK JM-JN JR-KG KI-KJ KN-KO KQ-KR KW KY-LE LG-LS LW-MN MP-NA NC-NQ NJ-NV NX-NY OA-OD OG-OH OK-OU OW-OX PB-PE PG PK PN-PO PT-PX PZ-QD QF-QG QJ-QQ QS-QT QW-QX RA RC-RG RI-RL RO-RZ SB-SC SF-SH SJ-TE TN TP-UJ UL-UM UQ-UR UT-UV VB VE-VH VJ VM VS-W WC WV-WX XB-XC XH-XJ XM-XT XX YA-YB YF-YG YJ YS-YX ZG-ZH ZN ZQ ZW AAA AAM-AAR AAZ-ABA ABF ABI-ABJ ABL-ABO ABT-ABW ABZ-ACC ACI ACM-ADA ADO ADF-ADH{
replace `x'="." if `x'=="NA"
}
but got an error message r(111) variables out of order.
I checked it once again but didn't find my error
Is it possible to write a loop for all variables and stata would change it only for string variables so that it wouldn't be type mismatch. I tried this command:
Code:
foreach x of varlist C-ADJ{
replace `x'="." if `x'=="NA",string
}

Comment