options(width = 120)

#*******************************************************************************#
#* Program: 1_harmonzd.tus_cps.1992.through.2023.r                              #
#* Date: September 2023                                                         #
#*                                                                              #
#* Purpose: R program to create permanent R dataset from the 1992-2023          #
#*          harmonized data file.                                               #
#*                                                                              #
#*   Notes: The program uses the package readr.                                 #
#*          The working directory location need to be modified                  #
#*          to match location of the data files and the format of the levels    #
#*          of the variables.                                                   #
#*          Formatted variables with level definitions are created as factors   #
#* Modifications                                                                #
#*                                                                              #
#*******************************************************************************#

##################################################################################
# Load libraries
#
library(readr)

##################################################################################
# Set working directory
##################################################################################

setwd("\\\\westat.com\\DFS\\NCIDCCPS\\DCCPS - Global\\Scientific Projects\\TUS-CPS\\Statistician\\ifc\\2025\\3. Read harmonized data\\3. R\\prg")
getwd()

##################################################################################
## Create data dictionary with variable names, widths, type and labels
##################################################################################


data_diccionary <-
'SurWave     1   2     n    "Survey wave"
SurYear     3   6     n    "Survey year"
SurMonth    7   8     n    "Survey month"
SpanHome    9   10    n    "Is Spanish the only language spoken by all members of this household who are 15 years of age or older"
HMOwnRnt    11  11    n    "Are your living quarters..."
FamInc      12  13    n    "Family income"
HHNum       14  15    n    "Number of persons in household"
OwnBsFrm    16  17    n    "Does anyone in this household have a business or a farm?"
Region      18  18    n    "Region"
FIPS        19  20    n    "State (FIPS Code)"
State       21  40    c    "State"
StCounty    41  45    n    "State/County"
MetStat     46  46    n    "Metropolitan status"
Age         47  48    n    "Age"
Sex         49  49    n    "Sex"
AFEver      50  51    n    "Did you ever serve on active duty in the U.S. Armed Forces"
Race92      52  52    n    "Race (1992-2002)"
Race03      53  54    n    "Race (2003 and later)"
DTAsian     55  56    n    "Detailed Asian Race Recode (2014 and later)"
Hispan      57  57    n    "Hispanic or non-Hispanic"
DTHisp92    58  59    n    "Origin or Descent (1992-2002)"
DTHisp03    60  61    n    "Detailed Hispanic Origin Group (2003-2011)"
DTHisp14    62  63    n    "Detailed Hispanic Origin Group (2014 and later)"
Marital     64  65    n    "Marital status"
COHab       66  66    n    "Cohabiting Partner (2010 and later)"
Edu         67  68    n    "Highest level of school completed or degree received"
EmplStat    69  70    n    "Employment status"
Natvty      71  73    n    "Country of birth"
NatvtyM     74  76    n    "Mother-s country of birth"
NatvtyF     77  79    n    "Father-s Country of birth"
Citizen     80  80    n    "Citizenship status"
YEARINUS    81  83    n    "Years in U.S."
AgeInUS     84  86    n    "Immigrant-s age of entry"
ABSNTRSN    87  88    n    "What is the main reason you were absent from work last week"
ABSNTPAY    89  90    n    "Are you being paid by your employer for any of the time off last week"
MULTJOBS    91  92    n    "Do you have more than one job"
OTWk        93  94    n    "Last week, did you work any overtime or extra hours (at your main job) that you do not usually work"
OTWkAMT     95  100   n    "Calculated weekly overtime amount"
CHILDNUM    101 102   n    "Number of children <18 years of age"
CHILD       103 104   n    "Presence of own children <18 years of age by selected age group"
CHILD2      105 106   n    "Any own children 0-2 years of age"
CHILD5      107 108   n    "Any own children 3-5 years of age"
CHILD13     109 110   n    "Any own children 6-13 years of age"
CHILD17     111 112   n    "Any own children 14-17 years of age"
WRKCLSS1    113 114   n    "Individual class of worker code on first job"
WRKCLSS2    115 116   n    "Individual class of worker code on second job"
IND1DT95    117 118   n    "Detailed industry recode - job 1 (1995-2002)"
IND1DT03    119 120   n    "Detailed industry recode - job 1 (2003 and later)"
IND2DT95    121 122   n    "Detailed industry recode - job 2 (1995-2002)"
IND2DT03    123 124   n    "Detailed industry recode - job 2 (2003 and later)"
OCC1DT95    125 126   n    "Detailed occupation recode - job 1 (1995-2002)"
OCC1DT03    127 128   n    "Detailed occupation recode - job 1 (2003 and later)"
OCC2DT95    129 130   n    "Detailed occupation recode - job 2 (1995-2002)"
OCC2DT03    131 132   n    "Detailed occupation recode - job 2 (2003 and later)"
IND1MJ95    133 134   n    "Major industry recode - job 1 (1995-2002)"
IND1MJ03    135 136   n    "Major industry recode - job 1 (2003 and later)"
IND2MJ95    137 138   n    "Major industry recode - job 2 (1995-2002)"
IND2MJ03    139 140   n    "Major industry recode - job 2 (2003 and later)"
OCC1MJ95    141 142   n    "Major occupation recode - job 1 (1995-2002)"
OCC1MJ03    143 144   n    "Major occupation recode - job 1 (2003 and later)"
OCC2MJ95    145 146   n    "Major occupation recode - job 2 (1995-2002)"
OCC2MJ03    147 148   n    "Major occupation recode - job 2 (2003 and later)"
RecordID    149 170   c    "Record ID (Unique within survey month)"
QSTNUM      171 175   n    "Unique household identifier (Unique within survey month)"
OCCURNUM    176 180   n    "Unique person identifier (Unique within survey month)"
Cig100      181 182   n    "Smoked 100 cigarettes in entire life"
CigAge      183 184   n    "Age first started smoking cigarettes fairly regularly"
CigAgeX     185 186   n    "Age first started smoking cigarettes (never smoked regularly)"
CGSTCRY     187 189   n    "State or country lived when first smoked part or all of a cigarette"
CGSTCRYX    190 192   n    "State or country lived when first smoked part or all of a cigarette (never smoked regularly)"
CIGNOW      193 194   n    "Now smoke cigarettes every day, some days or not at all"
CIGSTAT     195 196   n    "Current cigarette smoking status"
CPDD        197 198   n    "Number of cigarettes smoke a day (every day smokers)"
CPDD20      199 200   n    "On average, now smoke more or less than 20 cigarettes each day (every day smokers*)"
CIGDAY      201 202   n    "On how many of the past 30 days did you smoke cigarettes (some day smokers)"
CIGDAY20    203 204   n    "Would you say you smoked on at least 12 days in the past 30 days (some day smokers*)"
CPDS        205 206   n    "On days smoked, number of cigarettes smoke a day (some day smokers)"
CIGTYPE     207 208   n    "Type of cigarette (menthol/non-menthol, current smokers)"
TTFC        209 212   n    "Time to first cigarette, minutes (current smokers)"
TTFC30      213 214   n    "Smoke first cigarette of the day within first 30 minutes (current smokers)"
BUYCIG      215 216   n    "Do you usually buy your own cigarettes? (current smokers)"
BUYPKCR     217 218   n    "Do you usually buy your cigarettes by the pack or by the carton? (current smokers)"
PRICELPK    219 222   n    "Price paid for last pack of cigarettes (current smokers)"
PRICELCR    223 227   n    "Price paid for last carton of cigarettes (current smokers)"
DISCNTS     228 229   n    "Did you use coupons, rebates, or any other special promotions when you bought your last (pack/carton) of cigarettes"
CIGWHERE    230 231   n    "Where last (pack/carton) purchased (current smokers)"
CIGSTATE    232 233   n    "State where purchased last (pack/carton) (current smokers)"
CIGINDRS    234 235   n    "Buy last (pack/carton) of cigarettes from an Indian reservation (current smokers)"
CIGOTWAY    236 237   n    "Other way purchased last (pack/carton) of cigarettes (current smokers)"
SNGLCIG     238 239   n    "In the last 2 months, have you bought (or traded for) any single or individual cigarette (current smokers)"
SNGLPRCE    240 243   n    "Price paid for last  single or individual cigarette (current smokers)"
SNGLWHER    244 245   n    "Where last single or individual cigarette purchased (current smokers)"
SNGLSTCT    246 248   n    "State (or country) where purchased last single or individual cigarette (current smokers)"
LOOSETOB    249 250   n    "In the LAST 2 months, have you bought loose tobacco to roll-your-own cigarettes?"
YRSSMKDC    251 252   n    "Number of years smoked every day (every day smokers)"
YRSSMKDG    253 254   n    "For how long have you smoked every day (every day smokers)"
YRSSMKDH    255 257   n    "Number of years smoked every day (harmonized, every day smokers)"
MENTHREG    258 259   n    "Have you ever smoked menthol cigarettes for 6 months or more (current smokers)"
MENTHGRP    260 261   n    "For how long (have you smoked/did you smoke) menthol cigarettes (current smokers)"
MENTHOL     262 263   n    "Menthol use (constructed, current smokers)"
SDEVRDLY    264 265   n    "Ever smoked cigarettes every day for at least 6 months (some day smokers)"
SDEVRCIG    266 267   n    "When you last smoked every day, on average how many cigarettes did you smoke daily (some day smokers)"
SDYRSDLY    268 271   n    "Years since smoked every day (some day smokers)"
SDYRSMKC    272 273   n    "Years smoked every day (some day smokers)"
SDYRSMKG    274 275   n    "For how long have you smoked every day? (some day smokers)"
CIGSTA12    276 277   n    "Around this time 12 months ago, were you smoking cigarettes every day, some days or not at all? (current smokers)"
CPDD12      278 279   n    "Around this time 12 months ago, number of cigarettes smoked a day (current smoker, every day smokers 12 months ago)"
CIGDAY12    280 281   n    "Around this time 12 months ago, number of the past 30 days smoked cigarettes (some day smokers 12 months ago)"
CPDS12      282 283   n    "Around this time 12 months ago, number of cigarettes smoked a day (current smoker, some day smokers 12 months ago)"
EVSTP1DD    284 285   n    "Have you ever stopped smoking for one day or longer because you were trying to quit smoking? (every day smokers)"
PYSTP1DD    286 287   n    "During the past 12 months, have you stopped smoking for one day or longer because you were trying to quit smoking? (every day smokers)"
XPYSTPDN    288 289   n    "How many times during the past 12 months have you stopped smoking for one day or longer because you were trying to quit smoking? (every day smokers)"
XPYSTPDC    290 291   n    "How many times during the past 12 months have you stopped smoking for one day or longer because you were trying to quit smoking? (every day smokers)"
PYMLTDU     292 296   n    "During the past 12 months, what is the longest length of time you stopped smoking because you were trying to quit smoking? (every day smokers, number of months, not truncated)"
PYMLTD      297 300   n    "During the past 12 months, what is the longest length of time you stopped smoking because you were trying to quit smoking? (every day smokers, number of months)"
EVTRYQD     301 302   n    "Have you ever made a serious attempt to stop smoking because you were trying to quit - even if you stopped for less than a day? (every day smokers)"
PYTRYQD     303 304   n    "During the past 12 months, have you made a serious attempt to stop smoking because you were trying to quit <even if you stopped for less than a day>? (every day smokers)"
EVSTP1DS    305 306   n    "Have you ever stopped smoking for one day or longer because you were trying to quit smoking? (some days smokers, 12+ days)"
PYSTP1DS    307 308   n    "During the past 12 months, have you stopped smoking for one day or longer because you were trying to quit smoking? (some days smokers, 12+ days)"
XPYSTPSN    309 312   n    "How many times during the past 12 months have you stopped smoking for one day or longer because you were trying to quit smoking? (some days smokers, 12+ days)"
XPYSTPSC    313 314   n    "How many times during the past 12 months have you stopped smoking for one day or longer because you were trying to quit smoking? (some days smokers, 12+ days)"
PYMLTHSU    315 319   n    "During the past 12 months, what is the longest length of time you stopped smoking because you were trying to quit smoking? (some days smokers, 12+ days, number of months, not truncated)"
PYMLTHS     320 323   n    "During the past 12 months, what is the longest length of time you stopped smoking because you were trying to quit smoking? (some days smokers, 12+ days, number of months)"
EVTRYQS     324 325   n    "Have you ever made a serious attempt to stop smoking because you were trying to quit - even if you stopped for less than a day? (some days smokers, 12+ days)"
PYTRYQS     326 327   n    "During the past 12 months, have you made a serious attempt to stop smoking because you were trying to quit <even if you stopped for less than a day>? (some days smokers, 12+ days)"
PYQUITSD    328 329   n    "During the past 12 months, have you tried to quit smoking completely? (some days smokers, < 12 days)"
EVTRYQCS    330 331   n    "Have you ever tried to quit smoking completely? (some days smokers, < 12 days)"
PYMLTLSU    332 337   n    "During the past 12 months, what is the longest length of time you stopped smoking because you were trying to quit smoking? (some days smokers, < 12 days, number of months, not truncated)"
PYMLTLS     338 341   n    "During the past 12 months, what is the longest length of time you stopped smoking because you were trying to quit smoking? (some days smokers, < 12 days, number of months)"
PYQTNRT1    342 343   n    "Thinking back to the (last time/time) you tried to quit smoking in the past 12 months. Did you use a nicotine patch?"
PYQTNRT2    344 345   n    "Thinking back to the (last time/time) you tried to quit smoking in the past 12 months. Did you use a nicotine gum or nicotine lozenge?"
PYQTNRT3    346 347   n    "Thinking back to the (last time/time) you tried to quit smoking in the past 12 months. Did you use a nicotine nasal spray or nicotine inhaler?"
PYQTNRT4    348 349   n    "Thinking back to the (last time/time) you tried to quit smoking in the past 12 months. Did you use a prescription pill?"
PYQTHLP1    350 351   n    "Thinking back to the (last time/time) you tried to quit smoking in the past 12 months. Did you use a telephone help line or quit line?"
PYQTHLP2    352 353   n    "Thinking back to the (last time/time) you tried to quit smoking in the past 12 months. Did you use a stop smoking clinic, class or support group?"
PYQTHLP3    354 355   n    "Thinking back to the (last time/time) you tried to quit smoking in the past 12 months. Did you use one-on-one counseling?"
PYQTHLP4    356 357   n    "Thinking back to the (last time/time) you tried to quit smoking in the past 12 months. Did you use help or support from friends or family?"
PYQTHLP5    358 359   n    "Thinking back to the (last time/time) you tried to quit smoking in the past 12 months. Did you use the internet or World Wide Web/web-based program?"
PYQTHLP6    360 361   n    "Thinking back to the (last time/time) you tried to quit smoking in the past 12 months. Did you use books, pamphlets, videos or other materials?"
PYQTHLP7    362 363   n    "Thinking back to the (last time/time) you tried to quit smoking in the past 12 months. Did you use acupuncture or hypnosis?"
PYQHLP7A    364 365   n    "Thinking back to the (last time/time) you tried to quit smoking in the past 12 months. Which did you use acupuncture or hypnosis or both?"
PYQTPTN1    366 367   n    "The (last time/time) you tried to quit smoking in the past 12 months. Did you try to quit by gradually cutting back on cigarettes?"
PYQTPTN2    368 369   n    "The (last time/time) you tried to quit smoking in the past 12 months. Did you try to quit by switching to chewing tobacco, snuff, <snus>, cigars or pipes?"
PYQTPTN3    370 371   n    "The (last time/time) you tried to quit smoking in the past 12 months. Did you switch to a -lighter- cigarette in order to try to quit?"
PYQTPTN4    372 373   n    "The (last time/time) you tried to quit smoking in the past 12 months. Did you try to give up cigarettes all at once?"
PYQPTN4A    374 375   n    "The (last time/time) you tried to quit smoking in the past 12 months. When you tried to quit all at once, would you say you tried to quit -cold turkey-"
PYQTPTN5    376 377   n    "The (last time/time) you tried to quit smoking in the past 12 months. Did you try to quit by switching to electronic or e-cigarettes?"
DOCSEEN     378 379   n    "In the past year/12 months have you seen a medical doctor?"
DOCADVYR    380 381   n    "During the past year/12 months, did any medical doctor advise you to stop smoking?"
DOCADVQL    382 383   n    "In the past 12 months, when a medical doctor advised you to quit smoking, did the doctor also suggest that you call or use a telephone help line or quit line?"
DOCADVEV    384 385   n    "Has a medical doctor ever advised you to stop smoking?"
DDSSEEN     386 387   n    "In the past year/12 months have you seen a dentist?"
DDSADVYR    388 389   n    "During the past year/12 months, did any dentist advise you to stop smoking?"
DDSADVQL    390 391   n    "In the past 12 months, when a dentist advised you to quit smoking, did the dentist also suggest that you call or use a telephone help line or quit line?"
DDSADVEV    392 393   n    "Has a dentist ever advised you to stop smoking?"
INTQT6MO    394 395   n    "Are you seriously considering stopping/quitting smoking within the next 6 months?"
INTQT30D    396 397   n    "Are you planning to stop/quit within the next 30 days?"
INTRQUIT    398 399   n    "Overall, on a scale from 1 to 10 where 1 is not at all interested and 10 is extremely interested, how interested are you in quitting smoking?"
QUITSUCS    400 401   n    "If you did try to quit smoking altogether in the next 6 months, how likely do you think you would be to succeed..."
FMEVRDLY    402 403   n    "Ever smoked cigarettes every day for at least 6 months (former smokers)"
FMYRSDLY    404 408   n    "Years since smoked every day (former smokers who smoked every day for at least 6 months)"
FMEVRCIG    409 410   n    "When you last smoked every day, on average how many cigarettes did you smoke daily? (former smokers who smoked every day for at least 6 months)"
FMYRSMKC    411 412   n    "Years smoked every day (former smokers who smoked every day for at least 6 months)"
FMYRSMKG    413 414   n    "Years smoked every day (former smokers who smoked every day for at least 6 months)"
FMLOTQT     415 419   n    "About how long has it been since you completely stopped smoking cigarettes (years, former smokers)"
FMLOTQTC    420 421   n    "About how long has it been since you completely stopped smoking cigarettes (categorical, former smokers)"
FMCGST12    422 423   n    "Around this time 12 months ago, were you smoking cigarettes every day, some days or not at all? (former smokers who quit within the past year)"
FMCPDD12    424 425   n    "Around this time 12 months ago, on the average, about how many cigarettes did you smoke each day? (former smokers who quit within the past year, every day smokers around 12 months ago)"
FMCIGD12    426 427   n    "Around this time 12 months ago, on how many of 30 days in the month did you smoke cigarettes? (former smokers who quit within the past year, some day smokers around 12 months ago)"
FMCPDS12    428 429   n    "On the average, on those days, how many cigarettes did you usually smoke each day? (former smokers who quit within the past year, some day smokers around 12 months ago)"
FMMNTHUB    430 431   n    "Type of cigarette (menthol/non-menthol, former smokers who quit within the past 5 years/former smokers)"
FMMNTHRG    432 433   n    "Ever smoked menthol cigarettes (former smokers)"
FMMNTHGP    434 435   n    "How long did you smoke menthol cigarettes (former smokers who smoked menthol)"
FMQTNRT1    436 437   n    "When you quit smoking completely did you use a nicotine patch (former smokers who quit within the past [5,3] years)?"
FMQTNRT2    438 439   n    "When you quit smoking completely did you use a nicotine gum or nicotine lozenge? (former smokers who quit within the past [5,3] years)"
FMQTNRT3    440 441   n    "When you quit smoking completely did you use a nicotine nasal spray or nicotine inhaler? (former smokers who quit within the past [5,3] years)"
FMQTNRT4    442 443   n    "When you quit smoking completely did you use a prescription pill? (former smokers who quit within the past [5,3,1] year(s))"
FMQTHLP1    444 445   n    "When you quit smoking completely did you use a telephone help line or quit line? (former smokers who quit within the past [5,3,1] year(s))"
FMQTHLP2    446 447   n    "When you quit smoking completely did you use a stop smoking clinic, class or support group? (former smokers who quit within the past [5,3,1] year(s))"
FMQTHLP3    448 449   n    "When you quit smoking completely did you use one-on-one counseling? (former smokers who quit within the past [5,3,1] year(s))"
FMQTHLP4    450 451   n    "When you quit smoking completely did you use help or support from friends or family? (former smokers who quit within the past [5,3] years)"
FMQTHLP5    452 453   n    "When you quit smoking completely did you use the internet or World Wide Web/web-based program? (former smokers who quit within the past [5,3,1] year(s))"
FMQTHLP6    454 455   n    "When you quit smoking completely did you use books, pamphlets, videos or other materials? (former smokers who quit within the past [5,3] year(s))"
FMQTHLP7    456 457   n    "When you quit smoking completely did you use acupuncture or hypnosis? (former smokers who quit within the past [5,3] years)"
FMQHLP7A    458 459   n    "When you quit smoking completely which did you use acupuncture or hypnosis or both? (former smokers who quit within the past [5,3] years)"
FMQTPTN1    460 461   n    "When you quit smoking completely did you try to quit by gradually cutting back on cigarettes? (former smokers who quit within the past [5,3] years)"
FMQTPTN2    462 463   n    "When you quit smoking completely did you try to quit by switching to chewing tobacco, snuff, <snus>, cigars or pipes? (former smokers who quit within the past [5,3,1] year(s))"
FMQTPTN3    464 465   n    "When you quit smoking completely did you switch to a -lighter- cigarette in order to try to quit? (former smokers who quit within the past [5,3] years)"
FMQTPTN4    466 467   n    "When you quit smoking completely did you try to give up cigarettes all at once? (former smokers who quit within the past [5,3] years)"
FMQPTN4A    468 469   n    "When you quit smoking completely, when you tried to quit all at once, would you say you tried to quit -cold turkey-? (former smokers who quit within the past [5,3] years)"
FMQTPTN5    470 471   n    "When you quit smoking completely did you try to quit by switching to electronic or e-cigarettes? (former smokers who quit within the past [3,1] year(s))"
FMDOCSEE    472 473   n    "In the 12 months before you completely quit smoking did you see a medical doctor? (former smokers who quit within the past [3,1] year(s))"
FMDOCADV    474 475   n    "During the 12 months before you completely quit smoking, did any medical doctor advise you to stop smoking? (former smokers who quit within the past [3,1] year(s))"
FMTTFC      476 479   n    "During the year before your quit smoking, time to first cigarette, minutes (former smokers who quit within the past [5,3,1] year(s))"
FMTTFC30    480 481   n    "During the year before your quit smoking, smoke first cigarette of the day within first 30 minutes (former smokers who quit within the past [5,3,1] year(s))"
PIPEEVER    482 483   n    "Ever used pipes <regular pipe filled with tobacco>?"
CIGREVER    484 485   n    "Ever used cigars <regular cigar, cigarillos or little filtered cigars>?"
CHEWEVER    486 487   n    "Ever used chewing tobacco?"
SNFFEVER    488 489   n    "Ever used snuff?"
HOOKEVER    490 491   n    "Ever used a water or hookah pipe filled with tobacco?"
SMKLEVER    492 493   n    "Ever used smokeless tobacco?"
ECIGEVER    494 495   n    "Ever used e-cigarettes?"
PIPENOW     496 497   n    "Now use pipes <regular pipe filled with tobacco>?"
CIGRNOW     498 499   n    "Now use cigars <regular cigar, cigarillos or little filtered cigars>?"
CHEWNOW     500 501   n    "Now use chewing tobacco?"
SNFFNOW     502 503   n    "Now use snuff?"
HOOKNOW     504 505   n    "Now use a water or hookah pipe filled with tobacco?"
SMKLNOW     506 507   n    "Now use smokeless tobacco?"
ECIGNOW     508 509   n    "Now use e-cigarettes?"
PIPENOW2    510 511   n    "Now use pipes <regular pipe filled with tobacco>?"
CIGRNOW2    512 513   n    "Now use cigars <regular cigar, cigarillos or little filtered cigars>?"
CHEWNOW2    514 515   n    "Now use chewing tobacco?"
SNFFNOW2    516 517   n    "Now use snuff?"
HOOKNOW2    518 519   n    "Now use a water or hookah pipe filled with tobacco?"
SMKLNOW2    520 521   n    "Now use smokeless tobacco?"
ECIGNOW2    522 523   n    "Now use e-cigarettes?"
PIPECURR    524 525   n    "Current pipe <regular pipe filled with tobacco> use status"
CIGRCURR    526 527   n    "Current cigar <regular cigar, cigarillos or little filtered cigars> use status"
CHEWCURR    528 529   n    "Current chewing tobacco use status"
SNFFCURR    530 531   n    "Current snuff use status"
HOOKCURR    532 533   n    "Current water or hookah pipe filled with tobacco use status"
SMKLCURR    534 535   n    "Current smokeless tobacco use status"
ECIGCURR    536 537   n    "Current e-cigarette use status"
PIPESTAT    538 539   n    "Current pipe <regular pipe filled with tobacco> use status"
CIGRSTAT    540 541   n    "Current cigar <regular cigar, cigarillos or little filtered cigars> use status"
CHEWSTAT    542 543   n    "Current chewing tobacco use status"
SNFFSTAT    544 545   n    "Current snuff use status"
HOOKSTAT    546 547   n    "Current water or hookah pipe filled with tobacco use status"
SMKLSTAT    548 549   n    "Current smokeless tobacco use status"
ECIGSTAT    550 551   n    "Current e-cigarette use status"
PIPEDAY     552 553   n    "On how many of the past 30 days did you use pipes? (some day users)"
CIGRDAY     554 555   n    "On how many of the past 30 days did you use cigars? (some day users)"
CHEWDAY     556 557   n    "On how many of the past 30 days did you use chewing tobacco? (some day users)"
SNFFDAY     558 559   n    "On how many of the past 30 days did you use snuff? (some day users)"
HOOKDAY     560 561   n    "On how many of the past 30 days did you use a water or hookah pipe filled with tobacco? (some day users)"
SMKLDAY     562 563   n    "On how many of the past 30 days did you use smokeless tobacco? (some day users)"
ECIGDAY     564 565   n    "On how many of the past 30 days did you use e-cigarettes? (some day users)"
PIPELOTQ    566 569   n    "About how long has it been since you completely quit smoking a regular pipe filled with tobacco? (years, former pipe smokers)"
CIGRLOTQ    570 573   n    "About how long has it been since you completely quit smoking cigars, cigarillos or little filtered cigars? (years, former cigar smokers)"
HOOKLOTQ    574 577   n    "About how long has it been since you completely quit smoking a water or hookah pipe filled with tobacco? (years, former hookah smokers)"
SMKLLOTQ    578 581   n    "About how long has it been since you completely quit using smokeless tobacco? (years, former smokeless users)"
ECIGLOTQ    582 585   n    "About how long has it been since you completely quit using e-cigarettes? (years, former e-cigarette users)"
PIPEYRS     586 587   n    "In total, how many years (have you smoked/did you smoke) a regular pipe filled with tobacco? (ever pipe smokers)"
CIGRYRS     588 589   n    "In total, how many years (have you smoked/did you smoke) cigars or cigarillos or little filtered cigars? (ever cigar smokers)"
HOOKYRS     590 591   n    "In total, how many years (have you smoked/did you smoke) a water or hookah pipe filled with tobacco? (ever hookah smokers)"
SMKLYRS     592 593   n    "In total, how many years (have you used/did you use) smokeless tobacco? (ever smokeless users)"
ECIGYRS     594 595   n    "In total, how many years (have you used/did you use) e-cigarettes? (ever e-cigarette users)"
CIGRTYPE    596 597   n    "During the past 30 days, what type of cigar did you use most often?"
CIGRFLVR    598 599   n    "When you smoke a cigar is it usually flavored?"
PIPEFLVR    600 601   n    "When you smoke a regular pipe filled with tobacco is it usually flavored?"
HOOKFLVR    602 603   n    "When you smoke a water/hookah pipe filled with tobacco is it usually flavored"
SMKLFLVR    604 605   n    "When you use smokeless tobacco is it usually flavored"
ECIGFLVR    606 607   n    "When you use an e-cigarette is it usually flavored?"
ECIGBRDG    608 609   n    "Do you use e-cigarettes because you can use them at times when or in places where smoking cigarettes isn-t allowed?"
ECIGTOQT    610 611   n    "Do you use e-cigarettes to help you quit smoking cigarettes?"
ECIGTYPE    612 613   n    "Which of the following best describes the type of e-cigarette you currently use?"
BUYECIG     614 615   n    "Do you usually buy your own {e-cigarettes / e-cigarette <pods or> cartridges / e-liquid}? (current users)"
BUYPKECG    616 617   n    "Do you usually buy {e-cigarettes/e-cigarette <pods or> cartridges/bottles of e-liquid} by the box or pack, or as {single e-cigarettes/single e-cigarette <pods or> cartridges/a single bottle of e-liqui"
PRCEBOX     618 621   n    "What price did you pay for the LAST BOX OR PACK of {e-cigarettes / e-cigarette <pods or> cartridges / bottles of e-liquid} you bought? (current users)"
NUMECIGS    622 623   n    "How many {e-cigarettes / e-cigarette <pods or> cartridges / bottles of e-liquid} came in the LAST BOX OR PACK you bought? (current users)"
PRCESNGL    624 627   n    "What price did you pay for the LAST {e-cigarette / e-cigarette <pod or> cartridge / bottle of e-liquid} you bought? (current users)"
ECIGVOL     628 630   n    "How many MILLILITERS of liquid were in a SINGLE bottle you LAST bought? (current users)"
FMECIGBR    631 632   n    "Did you use e-cigarettes because you could use them at times when or in places where smoking cigarettes wasn-t allowed?"
FMECIGQT    633 634   n    "Did you use e-cigarettes to help you quit smoking cigarettes?"
TTFOTP      635 638   n    "Time to first other tobacco product, minutes (current users)"
TTFOTP30    639 640   n    "Use first other tobacco product of the day within first 30 minutes (current users)"
FMTTFOTP    641 644   n    "In the 12 months before you quit, time to first other tobacco product, minutes (former users)"
FMTTFO30    645 646   n    "In the 12 months before you quit, use first other tobacco product of the day within first 30 minutes (former users)"
PYSTPOTP    647 648   n    "During the past 12 months, have you stopped using other tobacco product for one day or longer because you were trying to quit? (every day and some days >11 users)"
PYLTOTP     649 652   n    "During the past 12 months, what is the longest length of time you stopped using other tobacco product because you were trying to quit? (every day and some days >11 users, number of months)"
WKAREA92    653 654   n    "Which of these best describes the area in which you work most of the time? (1992-2002 only)"
WKAREA03    655 656   n    "Which of these best describes the area in which you work most of the time? (2003-2007 only)"
WKINDPLC    657 658   n    "Indoor place mainly work (2003-2007 only)"
WKSTATE     659 660   n    "In which state, do you work on your main job or business (2003-2007 only)"
WKAREA10    661 662   n    "Do you mainly work indoors or outdoors? (2010 and later)"
WKINDPL1    663 664   n    "Indoor place mainly work (2010 and later)"
WKSTATE1    665 666   n    "In which state do you work (2010 and later)"
WKINDPL2    667 668   n    "Indoor place mainly work (2010 and later)"
WKSTATE2    669 670   n    "In which state do you work on your main indoor job or business (2010 and later)"
WKPOLICY    671 672   n    "Does your place of work have an official policy that restricts smoking in any way?"
WKPOLIND    673 674   n    "Smoking policy for indoor public or common areas such as lobbies, rest rooms and lunch rooms"
WKPOLWRK    675 676   n    "Smoking policy for work areas"
WORKBAN     677 678   n    "Smoking policy at work (constructed)"
WKSTPSMK    679 680   n    "Within the past 12 months, has your employer offered any stop smoking program or help to quit smoking"
WKSMK2WK    681 682   n    "During the past two weeks has anyone smoked in the area in which you work?"
WKPOLECG    683 684   n    "Which of these best describes the vaping or e-cigarette policy at your place of work for INDOOR PUBLIC OR COMMON AREAS AND INDOOR WORK AREAS? (2018 and later)"
WKSMK2EC    685 686   n    "During the PAST TWO WEEKS, has anyone vaped or used e-cigarettes in the area in which you work?"
ATSMKRST    687 688   n    "In restaurants, do you think that smoking should be:"
ATSMKHSP    689 690   n    "In hospitals, do you think that smoking should be:"
ATSMKIWA    691 692   n    "In indoor work areas, do you think that smoking should be:"
ATSMKBAR    693 694   n    "In bars and cocktail lounges (and clubs), do you think that smoking should be:"
ATSMKISP    695 696   n    "In indoor sporting events, do you think that smoking should be:"
ATSMKISM    697 698   n    "In indoor shopping malls, do you think that smoking should be:"
ATSMKPLY    699 700   n    "On outdoor children-s - playgrounds and sports fields, do you think that smoking should be:"
ATSMKCAS    701 702   n    "Inside casinos, do you think that smoking should be:"
ATSMCARO    703 704   n    "Inside a car, when there are other people present, do you think that smoking should..."
ATSMCARC    705 706   n    "If children are present inside the car, do you think that smoking should..."
HMSMKPOL    707 708   n    "Home smoking rules"
HMECGPOL    709 710   n    "Which statement best describes the rules about vaping or using e-cigarettes INSIDE YOUR HOME?"
MUHSMPOL    711 712   n    "In buildings with multiple apartments or living areas, do you think that smoking should be:"
ATMINBUY    713 714   n    "In your opinion, how easy is it for minors to buy cigarettes and other tobacco products in your community?"
ATSAMPLE    715 716   n    "Do you think that giving away free sample by tobacco companies should be:"
ATTOBADV    717 718   n    "Do you think advertising of tobacco products should be:"
SRWEIGHT    719 728   n    "Self Response Weight"'


##################################################################################
# Set location of TUS CPS 1992 through 2023 file
##################################################################################

Harmon_dat <- "../data/harmonzd.tus_cps.1992.through.2023.dat"

##################################################################################
# Create data dictionary
##################################################################################

tmp_file <- tempfile()
writeLines(data_diccionary, tmp_file)
data_diccionary_table <- read.table(tmp_file,sep="", col.names = c("variable", "initial_position","final_position", "type", "label"))

data_diccionary_tabletype <- as.character(data_diccionary_table$type)

### Create list for varaible name and type 
names(data_diccionary_tabletype) <-  data_diccionary_table$variable
data_diccionary_tabletype <- as.list(data_diccionary_tabletype)

##################################################################################
## Read TUS CPS 1992 through 2023 data
##


Harmon <- as.data.frame(read_fwf(Harmon_dat, 
                 col_positions = fwf_positions( data_diccionary_table$initial_position, 
                                 data_diccionary_table$final_position,
                                  col_names= data_diccionary_table$variable),
                 col_types = data_diccionary_tabletype  )) 

##################################################################################
## Attach labels
##################################################################################
for (i in 1:length(data_diccionary_table$variable))
{
  attr(Harmon[,i],"label") <- data_diccionary_table$label[i]
}
dim(Harmon)

##################################################################################
### Recode variables

Harmon$YEARINUS <- ifelse(!is.na(Harmon$YEARINUS), 
                          ifelse(Harmon$YEARINUS!=-1, Harmon$YEARINUS /10, Harmon$YEARINUS),
                                                      Harmon$YEARINUS)
Harmon$AgeInUS <- ifelse(!is.na(Harmon$AgeInUS), 
                          ifelse(!(Harmon$AgeInUS %in% c(-1,-9)), Harmon$AgeInUS /10, Harmon$AgeInUS),
                                                                  Harmon$AgeInUS)
Harmon$OTWkAMT <- ifelse(!is.na(Harmon$OTWkAMT), 
                          ifelse(Harmon$OTWkAMT!=-1, Harmon$OTWkAMT /100, Harmon$OTWkAMT),
                                                      Harmon$OTWkAMT)
Harmon$PRICELPK <- ifelse(!is.na(Harmon$PRICELPK), 
                          ifelse(!(Harmon$PRICELPK %in% c(-9, -3, -2, -1)), Harmon$PRICELPK /100, Harmon$PRICELPK),
                                                                            Harmon$PRICELPK)
Harmon$PRICELCR <- ifelse(!is.na(Harmon$PRICELCR), 
                          ifelse(!(Harmon$PRICELCR %in% c(-9, -3, -2, -1)), Harmon$PRICELCR /100, Harmon$PRICELCR),
                                                                            Harmon$PRICELCR)
Harmon$SNGLPRCE <- ifelse(!is.na(Harmon$SNGLPRCE), 
                          ifelse(!(Harmon$SNGLPRCE %in% c(-9, -3, -2, -1)), Harmon$SNGLPRCE /100, Harmon$SNGLPRCE),
                                                                            Harmon$SNGLPRCE)
Harmon$SDYRSDLY <- ifelse(!is.na(Harmon$SDYRSDLY), 
                          ifelse(!(Harmon$SDYRSDLY %in% c(-9, -3, -2, -1)), Harmon$SDYRSDLY /100, Harmon$SDYRSDLY),
                                                                            Harmon$SDYRSDLY)
Harmon$FMYRSDLY <- ifelse(!is.na(Harmon$FMYRSDLY), 
                          ifelse(!(Harmon$FMYRSDLY %in% c(-9, -3, -2, -1)), Harmon$FMYRSDLY /100, Harmon$FMYRSDLY),
                                                                            Harmon$FMYRSDLY)
Harmon$FMLOTQT <- ifelse(!is.na(Harmon$FMLOTQT), 
                          ifelse(!(Harmon$FMLOTQT %in% c(-9, -3, -2, -1)), Harmon$FMLOTQT /100, Harmon$FMLOTQT),
                                                                            Harmon$FMLOTQT)
Harmon$XPYSTPSN <- ifelse(!is.na(Harmon$XPYSTPSN), 
                          ifelse(!(Harmon$XPYSTPSN %in% c(-9, -3, -2, -1)), Harmon$XPYSTPSN /100, Harmon$XPYSTPSN),
                                                                            Harmon$XPYSTPSN)
Harmon$PYMLTHS <- ifelse(!is.na(Harmon$PYMLTHS), 
                          ifelse(!(Harmon$PYMLTHS %in% c(-9, -3, -2, -1)), Harmon$PYMLTHS /100, Harmon$PYMLTHS),
                                                                            Harmon$PYMLTHS)
Harmon$PYMLTLSU <- ifelse(!is.na(Harmon$PYMLTLSU), 
                          ifelse(!(Harmon$PYMLTLSU %in% c(-9, -3, -2, -1)), Harmon$PYMLTLSU /100, Harmon$PYMLTLSU),
                                                                            Harmon$PYMLTLSU)
Harmon$PYMLTLS <- ifelse(!is.na(Harmon$PYMLTLS), 
                          ifelse(!(Harmon$PYMLTLS %in% c(-9, -3, -2, -1)), Harmon$PYMLTLS /100, Harmon$PYMLTLS),
                                                                            Harmon$PYMLTLS)
Harmon$PYMLTHSU <- ifelse(!is.na(Harmon$PYMLTHSU), 
                          ifelse(!(Harmon$PYMLTHSU %in% c(-9, -3, -2, -1)), Harmon$PYMLTHSU /100, Harmon$PYMLTHSU),
                                                                            Harmon$PYMLTHSU)
Harmon$PYMLTD <- ifelse(!is.na(Harmon$PYMLTD), 
                          ifelse(!(Harmon$PYMLTD %in% c(-9, -3, -2, -1)), Harmon$PYMLTD /100, Harmon$PYMLTD),
                                                                          Harmon$PYMLTD)
Harmon$PYMLTDU <- ifelse(!is.na(Harmon$PYMLTDU), 
                          ifelse(!(Harmon$PYMLTDU %in% c(-9, -3, -2, -1)), Harmon$PYMLTDU /100, Harmon$PYMLTDU),
                                                                            Harmon$PYMLTDU)
Harmon$CIGRLOTQ <- ifelse(!is.na(Harmon$CIGRLOTQ), 
                          ifelse(!(Harmon$CIGRLOTQ %in% c(-99, -9, -3, -2, -1)), Harmon$CIGRLOTQ /100, Harmon$CIGRLOTQ),
                                                                                 Harmon$CIGRLOTQ)
Harmon$PIPELOTQ <- ifelse(!is.na(Harmon$PIPELOTQ), 
                          ifelse(!(Harmon$PIPELOTQ %in% c(-99, -9, -3, -2, -1)), Harmon$PIPELOTQ /100, Harmon$PIPELOTQ),
                                                                                 Harmon$PIPELOTQ)
Harmon$HOOKLOTQ <- ifelse(!is.na(Harmon$HOOKLOTQ), 
                          ifelse(!(Harmon$HOOKLOTQ %in% c(-99, -9, -3, -2, -1)), Harmon$HOOKLOTQ /100, Harmon$HOOKLOTQ),
                                                                                 Harmon$HOOKLOTQ)
Harmon$SMKLLOTQ <- ifelse(!is.na(Harmon$SMKLLOTQ), 
                          ifelse(!(Harmon$SMKLLOTQ %in% c(-99, -9, -3, -2, -1)), Harmon$SMKLLOTQ /100, Harmon$SMKLLOTQ),
                                                                                 Harmon$SMKLLOTQ)
Harmon$ECIGLOTQ <- ifelse(!is.na(Harmon$ECIGLOTQ), 
                          ifelse(!(Harmon$ECIGLOTQ %in% c(-99, -9, -3, -2, -1)), Harmon$ECIGLOTQ /100, Harmon$ECIGLOTQ),
                                                                                 Harmon$ECIGLOTQ)
Harmon$PRCEBOX <- ifelse(!is.na(Harmon$PRCEBOX), 
                          ifelse(!(Harmon$PRCEBOX %in% c(-9, -3, -2, -1)), Harmon$PRCEBOX /100, Harmon$PRCEBOX),
                                                                                                  Harmon$PRCEBOX)
Harmon$PRCESNGL <- ifelse(!is.na(Harmon$PRCESNGL), 
                          ifelse(!(Harmon$PRCESNGL %in% c(-9, -3, -2, -1)), Harmon$PRCESNGL /100, Harmon$PRCESNGL),
                                                                                                  Harmon$PRCESNGL)
Harmon$PYLTOTP <- ifelse(!is.na(Harmon$PYLTOTP), 
                         ifelse(!(Harmon$PYLTOTP %in% c(-99, -9, -3, -2, -1)), Harmon$PYLTOTP /100, Harmon$PYLTOTP),
                                                                                                    Harmon$PYLTOTP)
Harmon$SRWEIGHT <- Harmon$SRWEIGHT/10000



##################################################################################
# Attach levels of varaibles
source("./harmonzd.tus_cps.1992.through.2023.format_levels.r", echo=TRUE)

##################################################################################
## Save file Harmon as a RDS File
##################################################################################

str(Harmon, list.len=ncol(Harmon))

saveRDS(Harmon, file = "../data/Harmon.RDS")


