Skip to content. | Skip to navigation

Personal tools

DQI_R SAS Program for age 2 and older

** DQI_R.SAS **;
** This program was originally called 'csf9634.sas'  **;

** The following is an attempt to explain data set names and
 variable names used in this program. 

csf9602.ssd01 is the SAS data set of rt32.dat 
       (food item-level pyramid food group servings, ages 2 and up)
          sorted by hhid personid daycode seqnum.

The variable 'spnum' was renamed to 'personid'.
The variable 'dayct' is a created variable that is equal to 
 the number of days a subject reported eating food.  People who 
 fasted have a dayct equal to 1 despite the fact they reported two
 days of intake (comp_d1 and comp_d2 both equal to 1).
The variables 'eatday1' and 'eatday2' are created variables:
 eatday1=(d1_nrec>0);
 eatday2=(d2_nrec>0);

csf9603.ssd01 is the SAS data set of rt42.dat where each person has
        only 1 observation instead of 2 or 3 observations.
       (individual-level data, pyramid food group serving day1, 
        day2 totals, and 2-day averages for ages 2 and up)
          sorted by hhid personid

The following is documentation of the renamed variables for day1 totals:
         G_TOTAL  = grains1
         G_WHL    = grainsw1
         G_NWHL   = grainsn1
         V_TOTAL  = veges1
         V_DRKGR  = vegesdg1
         V_DPYEL  = vegesdy1
         V_LEGUME = vegesl1
         V_POTATO = vegesp1
         V_STARCY = vegess1
         V_TOMATO = vegest1
         V_OTHER  = vegeso1
         F_TOTAL  = fruit1
         F_CITMLB = fruitc1
         F_OTHER  = fruito1
         D_TOTAL  = dairy1
         D_MILK   = dairym1
         D_YOGURT = dairyy1
         D_CHEESE = dairyc1
         M_MPF    = meatpf1
         M_MEAT   = meatbp1
         M_ORGAN  = meato1
         M_FRANK  = meatfl1
         M_POULT  = meatp1
         M_FISH   = meatf1
         M_EGG    = meate1
         M_SOY    = meats1
         M_NUTSD  = meatns1
         DISCFAT  = dfat1
         ADD_SUG  = asugar1
         A_BEV    = alcnum1
         ENERGY   = kcal1
         TFAT     = fat1
         
The Day2 variables end with '2'.
The 2-day average variables begin with 'D2'.

csf9633.ssd01 is the SAS data set of rt40.dat where each person has
        only 1 observation instead of 2 or 3 observations.
        It contains day1, day2 total intake and 2-day average variables.

The following is documentation of the renamed variables for day1 totals:
   SODIUM   =   sodium1 
   CALCIUM  =   calc1 
   VITA_R   =   vare1 
   VITE     =	  ve1 
   VITC     =   vc1 
   FOLATE   =   fola1 
   ZINC     =   zinc1  
   SFAT     =   satf1  
   CHOLES   =   chol1
   R_IRON   =   rdairon1

The Day2 variables end with '2'.
The 2-day average variables begin with 'D2'.

******************************************************************;


***********************************************************************
Program: csf9634                            Stream: /usda/csf96
Input: /csf96/data/csf9602                  Output: csf9634 
       /csf96/data/csf9603   
       /csf96/data/csf9633
       /docs/foodcode.txt

Programmer: Dan Blanchette            Date: 11Dec1997
Modified:   Dan Blanchette            Date: 01Sep1999
Last run:   Dan Blanchette            Date: 01Sep1999

* Create DQI-Revised.                                                 *
* Create Diet Variety Scores from food level file.                    *
* Create Moderation Scores from Day totals and 2-Day average file.    *
*        Ages 2 and over                                              *
*                                                                     *
*       Fasters and Day1 only people will have 2-day avgs that are    *
*        the same as their day1 or day2 totals                        * 
*      "where dayct=1 and comp_d1=1 and comp_d2=1" selects the fasters*
*                                                                     *
* Search foodcode descriptions for food group words to assist         *
*  in food grouping of grains servings.                               *
*                                                                     *
***********************************************************************;
%let job=/progs/csf9634;
%let land=ps=65 ls=240 nocenter;
%let land1=ps=65 ls=175 nocenter;
%let por=ps=65 ls=86 center;

options &land;
libname data "../data";

title1 "1994-96 CSFII Stream: Diet Quality Index - Revised";
title2 "Ages 2 and over";
footnote "program &job"; 

filename in "../docs/foodcode.txt";

*** create data set for description of foodcodes for keyword search **;
data fooddesc(drop=varlen);
  infile in length=linelen;
  input @1 foodcode 1-8 @;
  varlen=linelen-10;
  input @11 fooddesc $varying60. varlen;
label
 foodcode='Food code'
 fooddesc='8-digit, 60 char food description';
run;
proc print data=fooddesc (obs=20);
title3 "data=fooddesc: sample output of food descriptions";
run;

proc format;
value 
 grainnum
   1="1=GRAIN"
   3="3=PASTA"
   4="4=RICE"
   5="5=CEREAL" 
   7="7=QK BREAD"
;;;;
** The following is an expanded version of the same format.
 grainnum
   1="1=WHOLE GRAIN BREADS, ROLLS, CRACKERS, ETHNIC MIXTURES"
   2="2=NON-WHOLE GRAIN BREADS, ROLLS, CRACKERS, ETHNIC MIXTURES"
   3="3=PASTA"
   4="4=RICE"
   5="5=WHOLE GRAIN CEREALS" 
   6="6=NON-WHOLE GRAIN CEREALS" 
   7="7=QUICK BREADS"
;;;;
run;

data found2 notfound fooddesc;
 set fooddesc;

graingrp=0; *** sets food group var  ***; 

*** Search for keywords in food description to assign the appropriate 
          value to graingrp.*** ;
%MACRO SEARCH(word,fdgrpno);
**** if index(fooddesc,"RICE") then graingrp=1;
if index(fooddesc,upcase("&word")) then graingrp=&fdgrpno;

%MEND;
/* Grains */
%SEARCH(english muffin,1);
%SEARCH(chimichanga,1);
%SEARCH(quesadilla,1);
%SEARCH(nacho,1);
%SEARCH(egg roll,1);
%SEARCH(croissant,1);
%SEARCH(pot pie,1);
%SEARCH(BURRITO,1);
%SEARCH(TACO,1);    
%SEARCH(enchilada,1);    
%SEARCH(PIZZA,1);    
%SEARCH(BUN,1);
%SEARCH(fajita,1);
%SEARCH(tortilla,1);
%SEARCH(SAND,1);
%SEARCH(DUMPL,1);
%SEARCH(STUFFI,1);

/* Pasta */
%SEARCH(rigaton,3);
%SEARCH(NOOD,3);
%SEARCH(tetrazzin,3);
%SEARCH(MACARON,3);
%SEARCH(lo mein,3);
%SEARCH(gnocch,3);
%SEARCH(cannellon,3);
%SEARCH(TORTEL,3); 
%SEARCH(manicott,3);
%SEARCH(linguin,3);    
%SEARCH(fettuccin,3);
%SEARCH(spaghetti,3);
%SEARCH(stuffed shell,3);
%SEARCH(mosticolli,3);
%SEARCH(pasta,3);
%SEARCH(raviol,3);
%SEARCH(lasagna,3);

/* Rice */
%SEARCH(sushi,4);
%SEARCH(RICE,4);
%SEARCH(paella,4);
%SEARCH(stuffed pepper,4);

/*Quick Breads*/
%SEARCH(biscuit,7);

if graingrp>0 then output found2;
else if graingrp=0 and (58000000<=foodcode<59000000) then output notfound;
else output fooddesc;

format graingrp grainnum.; 
label 
 graingrp = "Grain Group assignment for DVS"
;;;
run;

data fooddesc found1;
 set fooddesc;

graingrp=0; *** sets food group var  ***; 

**Grain;
if foodcode in(
27246300,
27250030,
27250040,
27250050,
27250070,
27250160,
27250210,
27250300,
27513040,
27515000,
27515020,
27515070,
27520170,
27520250,
27520380,
27520390,
27520500,
27550750,
27560300,
27560350,
27560650,
27560670,
27570310) or 
foodcode in(58120110,58120120) 
then graingrp=1;  

**Pasta; 
if 56100000<=foodcode<56200000 then graingrp=3;

**Rice;
if foodcode in(27213120,27213420) then graingrp=4;

**Crackers;
if 54000000<=foodcode<55000000 then graingrp=1;

**Cereal;
if 56200000<=foodcode<56204000 
or 56206000<=foodcode<56300000 
or 57000000<=foodcode<58000000 then graingrp=5;

**Quick Breads;
if 51160000<=foodcode<51166000 
or 52000000<=foodcode<53000000 
or 55000000<=foodcode<56000000 then graingrp=7;

** fix bad matches;
***Grains;
if (51000000<=foodcode< 51160000) or
   (51166000<=foodcode< 52000000) or
   (58101200<=foodcode<=58117510) or
   (58122250<=foodcode<=58127150) or
   (58128210<=foodcode<=58128250) or
   foodcode in(58175110,58310310,58401010,58401200,58404500) then graingrp=1;

**Pasta;
if foodcode in(58122210,58408010) then graingrp=3;

if graingrp>0 then output found1;
else output fooddesc;
run;

** concatenate data sets;
data fooddesc;
 set found1 found2; by foodcode;

** weed out bad matches  **;
if index(fooddesc,upcase("licorice"))
 or index(fooddesc,upcase("no rice"))
 or index(fooddesc,upcase("no noodles"))
 or index(fooddesc,upcase("spaghetti sauce"))
 or index(fooddesc,upcase("enchilada sauce"))
 or index(fooddesc,upcase("squash, spaghetti"))
 or index(fooddesc,upcase("pizza cheese"))
 or index(fooddesc,upcase("pizza mate"))
 or index(fooddesc,upcase("sandwich loaf"))
 or index(fooddesc,upcase("sandwich spread"))
 or index(fooddesc,upcase("thousand"))
 or index(fooddesc,upcase("cookie"))
 or (53450000<=foodcode<=53451000)
 or foodcode in(58118110,
                58118210,
                58149210,
                58156610,
                58310110,
                58400000,
                58407000,
                58407020,
                58407070,
                58421000)  
 or foodcode<14000000 
 or foodcode>90000000 
 then graingrp=0;

** assure that any cereal is a cereal, eg. rice krispies cereal not "rice";
 if index(fooddesc,upcase("cereal")) then graingrp=5;
run;

proc freq data=fooddesc;
tables graingrp;
title3 "data=fooddesc: check creation of graingrp";
run;

proc sort data=fooddesc out=testgrp; by graingrp foodcode;
run;

data testgrp;
 set testgrp;
         by graingrp;
where graingrp>0;
retain count;
if first.graingrp then count=0;
count=count+1;
if count<=20 then output ;
run;

proc print data=testgrp;
by graingrp;
title3 "data=testgrp: sample output of grain grouping ";
title4 " (first 20 obs of each group)";
run;

proc sort data=data.csf9602 
         (keep=hhid     personid daycode  foodcode age dayct eatday1 eatday2 g_total  g_whl    g_nwhl 
               v_drkgr  v_dpyel  v_legume v_potato v_starcy v_tomato v_other d_milk   d_yogurt d_cheese
               m_meat   m_organ  m_frank  m_poult  m_fish m_egg m_soy f_citmlb f_other  )
 out=csf9602;
  by foodcode;
run;

data csf9602;
merge csf9602(in=a) fooddesc(in=b); by foodcode;
if a=1 then output csf9602; ** only want foods consumed in 1994-96 **;
run;

proc sort data=csf9602; by hhid personid daycode foodcode;
run;

/*
*Sample output;
options &land;
proc print data=csf9602 (obs=100);
title3 "1st 100 obs of csf9602";
run;
*/

** Create diet variety scores.  Add up day totals. ***;
data csf9634 (drop=i foodcode graingrp
               g_total  g_whl    g_nwhl 
               v_drkgr  v_dpyel  v_legume v_potato v_starcy v_tomato v_other
               d_milk   d_yogurt d_cheese 
               m_meat   m_organ  m_frank  m_poult m_fish m_egg m_soy
               f_citmlb f_other
               v_legsoy m_meorfr d_milkyo
               fcamt1-fcamt22)
     test1; ** check how at least 1/2 serving is being created **;
 set csf9602 (drop=fooddesc); by hhid personid daycode foodcode;
 retain dvg1_1-dvg1_7   /*Grains*/
        dvv1_1-dvv1_7   /*Veges*/
        dvdm1_1-dvdm1_6 /*Dairy & Meat*/
        dvf1_1-dvf1_2   /*Fruit*/
        fcamt1-fcamt22;

**Combine legumes and soy servings per foodcode;
v_legsoy=sum(v_legume,m_soy);

**Combine meat, organ, and frank servings per foodcode;
m_meorfr=sum(m_meat,m_organ,m_frank);

**Combine Milk and Yogurt;
d_milkyo=sum(d_milk,d_yogurt);

 array vege  [7] v_drkgr  v_dpyel  v_legsoy v_potato v_starcy  v_tomato v_other; ** Vegetables;
 array dame  [6] d_milkyo d_cheese m_meorfr m_poult   m_fish m_egg;              ** Dairy & Meats;
 array fruit [2] f_citmlb f_other;                                               ** Fruit;

      ** day1 Grain **;
 array dvgrn1  [7] dvg1_1-dvg1_7; 
      ** day1 Vege **;
 array dvvege1 [7] dvv1_1-dvv1_7;
      ** day1 Dairy & Meat **;
 array dvdame1 [6] dvdm1_1-dvdm1_6;
      ** day1 Fruit **;
 array dvfrut1 [2] dvf1_1-dvf1_2;

 array fcamnt [22] fcamt1-fcamt22;
 array serving [22] g_whl    g_nwhl   g_total  g_total  g_whl    g_nwhl   g_total
                    v_drkgr  v_dpyel  v_legsoy v_potato v_starcy v_tomato v_other
                    d_milkyo d_cheese m_meorfr m_poult  m_fish   m_egg
                    f_citmlb f_other;

** Sum up servings of the same foodcodes eaten in the same day **;

 ** resets for each change in foodcode or change in day, person, etc.;
if first.foodcode then do i=1 to 22;
  fcamnt[i]=0; 
end;

 do i=1 to 22;
  fcamnt[i]=serving[i]+fcamnt[i];  *sum up servings of same foodcode;
 end;
** resets the value of the serving vars to foodcode total number of servings;
 if last.foodcode then do i=1 to 22;
  serving[i]=fcamnt[i];  
 end;

** creates Day1 and Day2 totals. ***; 
 if first.daycode then do i=1 to 7;
   if (i<=7) then dvgrn1[i]=0;   ** resets to initial values  **;
   if (i<=7) then dvvege1[i]=0;             
   if (i<=6) then dvdame1[i]=0;
   if (i<=2) then dvfrut1[i]=0;
 end;

if last.foodcode then do;

 ** For Day1 and Day2 totals: **;
 ** Create dummy vars (=0 or =1) for each food group: if .5<=7) then dvgrn1[i] =.;
  if (i<=7) then dvvege1[i]=.;
  if (i<=6) then dvdame1[i]=.;
  if (i<=2) then dvfrut1[i]=.;
 end;
output csf9634; 
run;
** csf9634 should have 1 to 2 obs per person **;

proc print data=test1(obs=50);
 title3 "test1: check how at least 1/2 serving is being created";
run;

** delete data sets testgrp test1 **;
proc datasets library=work memtype=data;
             delete testgrp test1;
run;

proc print data=csf9634(obs=30);
title3 "data=csf9634: check day1 and day2 totals of servings ";
run;

** create a 2-day average buy summing day1 and day2 **;
** person gets credit for 2-day avg. if they got credit on day1 or day2 **;
proc summary nway data=csf9634;
 by hhid personid;
var dvg1_1-dvg1_7
    dvv1_1-dvv1_7
    dvdm1_1-dvdm1_6
    dvf1_1-dvf1_2;
id age dayct eatday1 eatday2;
output out= day_avg(drop=_type_ _freq_)
       sum= dvg1_1-dvg1_7
            dvv1_1-dvv1_7
            dvdm1_1-dvdm1_6
            dvf1_1-dvf1_2
           ;;;
run;

proc print data=day_avg(obs=30);
title3 "data=day_avg: after proc summary of day1 and day2";
run;

data day_avg;
 set day_avg;
daycode=4;  ** USDA uses "4" to indicate the 2-day avg. **;
run;

** add on obs from day_avg ***;
data csf9634(drop=i);
 set csf9634
     day_avg; by hhid personid daycode;

array fix[22] dvg1_1-dvg1_7
              dvv1_1-dvv1_7
              dvdm1_1-dvdm1_6
              dvf1_1-dvf1_2
             ;;;

if daycode=4 then do i=1 to 22;
 if fix[i]>1 then fix[i]=1;
 else
 if fix[i]=. then flag=1; *** fix[i]=. shouldn't happen;
end;

run;

proc print data=csf9634(obs=30);
title3 "data=csf9634: creation of 2-day avg";
run;

proc freq data=csf9634;
tables flag;
title3 "data=csf9634: check for flag=1";
run;
** flag should be missing for all obs **;
 
data csf9634(drop=i);
 set csf9634(drop=flag);

 array dvgrn1  [7] dvg1_1-dvg1_7;   ** day1 Grain        **;
 array dvvege1 [7] dvv1_1-dvv1_7;   ** day1 Vege         **;
 array dvdame1 [6] dvdm1_1-dvdm1_6; ** day1 Dairy & Meat **;
 array dvfrut1 [2] dvf1_1-dvf1_2;   ** day1 Fruit        **;

** sum across each array to create diet variety total scores  ***;
** initialize vars to add up diversity food group vars day1, day2 totals and 2-day average ***;
    dvsg1=0;  
    dvsv1=0;
    dvsdm1=0;
    dvsf1=0;

   do i=1 to 7;
       *** the sum function is used so that missings are not kept missing ;
    if (i<=7) then dvsg1=sum(dvsg1,dvgrn1[i]); 
    if (i<=7) then dvsv1=sum(dvsv1,dvvege1[i]);
    if (i<=6) then dvsdm1=sum(dvsdm1,dvdame1[i]);
    if (i<=2) then dvsf1=sum(dvsf1,dvfrut1[i]);
   end;

** did not eat on day1 should not have day1 total
  and likewise for day2;
if (daycode=1 and eatday1=0) or
   (daycode=2 and eatday2=0) then do; 
    dvsg1=.;
    dvsv1=.;
    dvsdm1=.;
    dvsf1=.;
end;

*Create Diet Variety Score;
    dvs1=dvsg1
        +dvsv1
        +dvsdm1
        +dvsf1;

 ** Create Diet Variety Score as a 10pt score **;
  * (the denominator is the number of components of that scores subgroup);  
    dvswtd1=2.5*(dvsg1/7)
           +2.5*(dvsv1/7)
           +2.5*(dvsdm1/6)
           +2.5*(dvsf1/2);  
run;

*Sample output;
options &land;
proc print data=csf9634 (firstobs=101 obs=200);
title3 "data=csf9634: sample output ";
run;

*** create a data set with 2 to 3 obs per person, day1, day2 totals and 2-day avg. nutrients **;
data csf9633(keep=hhid personid eatday1 eatday2 dayct daycode 
                  kcal1 fat1   sodium1  calc1  vare1  ve1  vc1  fola1  zinc1  satf1  chol1  rdairon1
                );
set data.csf9633(keep=hhid personid eatday1 eatday2 dayct
                  kcal1  fat1  sodium1  calc1  vare1  ve1  vc1  fola1  zinc1  satf1  chol1  rdairon1 
                  kcal2  fat2  sodium2  calc2  vare2  ve2  vc2  fola2  zinc2  satf2  chol2  rdairon2 
                  d2kcal d2fat d2sodium d2calc d2vare d2ve d2vc d2fola d2zinc d2satf d2chol d2rdairo
                );
       by hhid personid;

  array tot1[12]  kcal1  fat1  sodium1  calc1  vare1  ve1  vc1  fola1  zinc1  satf1  chol1  rdairon1 ;
  array tot2[12]  kcal2  fat2  sodium2  calc2  vare2  ve2  vc2  fola2  zinc2  satf2  chol2  rdairon2 ;
  array d2avg[12] d2kcal d2fat d2sodium d2calc d2vare d2ve d2vc d2fola d2zinc d2satf d2chol d2rdairo ;

do i=1 to 3;
 if i=1 then do;
  daycode=1;
  output;
 end;
 if i=2 then do;
  do j=1 to 12;
   tot1[j]=tot2[j];
   end;
  daycode=2;
  output;
 end;
 if i=3 then do;
  do j=1 to 12;
   tot1[j]=d2avg[j];
   end;
  daycode=4;
  output;
 end;
end; ** end of "do i=1 to 3" do loop;
run;
** csf9633 should have 3 obs per person ***;

proc print data=csf9633 (obs=30);
title3 "data=csf9633: after it was made into 3 obs per person ";
run;

*** create a data set with 2 to 3 obs per person, day1, day2 totals and 2-day avg. servings **;
data csf9603(keep=hhid personid gender daycode grains1 meats1  veges1 fruit1  alcnum1  asugar1  dfat1
                        comp_d1 comp_d2       );
 set data.csf9603 (keep=hhid personid gender   grains1 meats1  veges1 fruit1  alcnum1  asugar1  dfat1 
                        comp_d1 comp_d2        grains2 meats2  veges2 fruit2  alcnum2  asugar2  dfat2 
                                               d2grain d2meats d2vege d2fruit d2alcnum d2asugar d2dfat
                                              );
   array tot1[7]  grains1 meats1  veges1 fruit1  alcnum1  asugar1  dfat1 ;
   array tot2[7]  grains2 meats2  veges2 fruit2  alcnum2  asugar2  dfat2 ;
   array d2avg[7] d2grain d2meats d2vege d2fruit d2alcnum d2asugar d2dfat;

do i=1 to 3;
 if i=1 then do;
   daycode=1;
   output;
 end;
 if i=2 then do;
  daycode=2;
  do j=1 to 7;
   tot1[j]=tot2[j];
   end;
  output;
 end;
 if i=3 then do;
  daycode=4;
  do j=1 to 7;
   tot1[j]=d2avg[j];
   end;
  output;
 end;
end; ** end of "if do i=1 to 3" do loop;
run;
** csf9603 should have 3 obs per person ***;

proc print data=csf9603 (obs=30);
title3 "data=csf9603: after it was made into 3 obs per person ";
run;

data csf9634;  
 merge csf9603(in=a)
       csf9634(in=b) 
       csf9633(in=c keep=hhid personid daycode eatday1 eatday2 dayct
                kcal1 fat1 sodium1 calc1 vare1 ve1 vc1 fola1 zinc1 satf1 chol1 rdairon1 
                );
       by hhid personid daycode;
if a=1;

** create percent ai calcium;
 paicalc1=((calc1/500) *(2<=age<=3)
          +(calc1/800) *(4<=age<=8)
          +(calc1/1300)*(9<=age<=13)
          +(calc1/1300)*(14<=age<=18)
          +(calc1/1000)*(19<=age<=50)
          +(calc1/1200)*(51<=age<=99))*100;

** create ai calcium score;
 aicscr1=round(paicalc1/10,.01); ** round to 100ths;
 if aicscr1>10 then aicscr1=10;  ** cap at 10 points;

** create percent rda iron and rda iron score;
** make rdairon1 a true percent like r_iron is in rt42 **;
 rdairon1=rdairon1*100;  
 
rdaiscr1=round(rdairon1/10,.01); ** round to 100ths;
  if rdaiscr1>10 then rdaiscr1=10;  ** cap at 10pts.;

** Create enerfat, enersfat for Day-1, Day-2 and 2-Day intake **;
     enerfat1=.;
     enersfa1=.;
     if kcal1=0 then do;
        enerfat1=0;
        enersfa1=0;
     end;
     else if kcal1>0 then do;
        enerfat1=((fat1*9)/kcal1)*100;
        enersfa1=((satf1*9)/kcal1)*100;
     end;

  ** Create categorical variables from continuous vars to be **;
  ** use to calculate the DQI;                               **;

   if 0<=enerfat1<=30 then efatscr1=10;
      else if 30<=40 then efatscr1=5;
      else if 40<=enersfa1<=10 then esftscr1=10;

      else if 10<=13 then esftscr1=5;
      else if 13<=chol1<=300 then cholscr1=10;
      else if 300<=400 then cholscr1=5;
      else if 40010 then dqigscr1=10;           ** cap at 10 pts.;

 *Vegetables;
  dqivpct1=(veges1/&veg_min)*100;
  dqivscr1=round((veges1/&veg_min)*10,.01);
  if dqivscr1>10 then dqivscr1=10;

 *Fruits;
  dqifpct1=(fruit1/&frt_min)*100;
  dqifscr1=round((fruit1/&frt_min)*10,.01);
  if dqifscr1>10 then dqifscr1=10;

 *Sugar*;
  modspct1=(asugar1/&sug_max)*100;
  modsscr1=(  .<=100)*2.5
          +(100<=150)*1.5
          +(150<=200)*1.0
          +(200<=1800 then do;
         %dqi(6,2,3,6);
    end;
else
if 1800<=2600 then do;
         %dqi(9,3,4,12);
    end;
else
if 2600<=99999 then do;
         %dqi(11,4,5,18);
    end;

**Create Moderation Scores;
**Alcohol moderation scores not modified by age ***;

**Alcohol;
if (.<=100)*2.5
            +(100<=150)*1.5
            +(150<=200)*1.0
            +(2000 then do;
   alcdrnk1=1;  ** create if person drinks alcohol or not;
   adrink1=alcnum1; ** create # of alcohol drinks if person drinks; 
  end;
 else do;
   alcdrnk1=0;
   adrink1=.;
  end;

end;  *** end of (.<=2400)*2.5
         +(2400<=3400)*1.5
         +(3400<10 then 
   put hhid personid daycode= '  ' kcal1= '  ' modsscr1= '  ' modspct1= '  ' asugar1=; 
  if count<10 then 
   put hhid personid daycode= '  ' modascr1= '  ' modapct1= '  ' alcnum1= '  ' alcdrnk1= '  ' adrink1=;
  if count<10 then 
   put hhid personid daycode= '  ' modnas1= '  '  sodium1=;

* Create Moderation Score for Discretionary Fat;
 if dfat1>. then do;
  moddfs1=( .< dfat1< 25)*2.5
         +(25<=dfat1<=50)*1.5
         +(50< dfat1< 75)*1.0
         +(75<=dfat1    )*0;
 end; ** end of dfat1>. do loop;

* Create Moderation Score (a 10 point score) ;
     ms1=modsscr1
        +modascr1
        +modnas1
        +moddfs1;

* Create Diet Quality Index Revised (100pt Score);
  dqi_r1=efatscr1
        +esftscr1
        +cholscr1
        +dqigscr1
        +dqivscr1
        +dqifscr1
        +aicscr1 
        +rdaiscr1
        +dvswtd1
        +ms1;

**Check program;
if count<10 then 
 put hhid personid daycode= '  ' dfat1= '  ' moddfs1= ;
if count<10 then 
 put hhid personid daycode= '  ' ms1= '   '  modsscr1= '  '  modascr1= '  '  modnas1= '  '  moddfs1=;
run;

proc print data=csf9634 (obs=50);
title3 "data=csf9634: check creation of all vars";
run;


** return data set to 1 obs per person  **;
data csf96341(drop=daycode)
     csf96342(drop=daycode)
     csf96344(drop=daycode);
 set csf9634(keep=hhid personid daycode age gender dayct comp_d1  comp_d2
                  dvg1_1   dvg1_2   dvg1_3   dvg1_4   dvg1_5   dvg1_6   dvg1_7
                  dvv1_1   dvv1_2   dvv1_3   dvv1_4   dvv1_5   dvv1_6   dvv1_7
                  dvdm1_1  dvdm1_2  dvdm1_3  dvdm1_4  dvdm1_5  dvdm1_6  dvf1_1
                  dvf1_2   dvsg1    dvsv1    dvsdm1   dvsf1    dvs1     dvswtd1
                  paicalc1 aicscr1  rdaiscr1 enerfat1 enersfa1 efatscr1 esftscr1
                  cholscr1 dqigpct1 dqigscr1 dqivpct1 dqivscr1 dqifpct1 dqifscr1
                  modspct1 modsscr1 modapct1 modascr1 modnas1  moddfs1  alcdrnk1
                  adrink1  ms1      dqi_r1   );
if daycode=1 then output csf96341;
if daycode=2 then output csf96342;
if daycode=4 then output csf96344;
run;

data csf96342(drop= i dvg1_1   dvg1_2   dvg1_3   dvg1_4   dvg1_5   dvg1_6   dvg1_7
                      dvv1_1   dvv1_2   dvv1_3   dvv1_4   dvv1_5   dvv1_6   dvv1_7
                      dvdm1_1  dvdm1_2  dvdm1_3  dvdm1_4  dvdm1_5  dvdm1_6  dvf1_1
                      dvf1_2   dvsg1    dvsv1    dvsdm1   dvsf1    dvs1     dvswtd1
                      paicalc1 aicscr1  rdaiscr1 enerfat1 enersfa1 efatscr1 esftscr1
                      cholscr1 dqigpct1 dqigscr1 dqivpct1 dqivscr1 dqifpct1 dqifscr1
                      modspct1 modsscr1 modapct1 modascr1 modnas1  moddfs1  alcdrnk1
                      adrink1  ms1      dqi_r1   );
 set csf96342;

array allday1[52] dvg1_1   dvg1_2   dvg1_3   dvg1_4   dvg1_5   dvg1_6   dvg1_7
                  dvv1_1   dvv1_2   dvv1_3   dvv1_4   dvv1_5   dvv1_6   dvv1_7
                  dvdm1_1  dvdm1_2  dvdm1_3  dvdm1_4  dvdm1_5  dvdm1_6  dvf1_1
                  dvf1_2   dvsg1    dvsv1    dvsdm1   dvsf1    dvs1     dvswtd1
                  paicalc1 aicscr1  rdaiscr1 enerfat1 enersfa1 efatscr1 esftscr1
                  cholscr1 dqigpct1 dqigscr1 dqivpct1 dqivscr1 dqifpct1 dqifscr1
                  modspct1 modsscr1 modapct1 modascr1 modnas1  moddfs1  alcdrnk1
                  adrink1  ms1      dqi_r1   ;

array allday2[52] dvg2_1   dvg2_2   dvg2_3   dvg2_4   dvg2_5   dvg2_6   dvg2_7
                  dvv2_1   dvv2_2   dvv2_3   dvv2_4   dvv2_5   dvv2_6   dvv2_7
                  dvdm2_1  dvdm2_2  dvdm2_3  dvdm2_4  dvdm2_5  dvdm2_6  dvf2_1
                  dvf2_2   dvsg2    dvsv2    dvsdm2   dvsf2    dvs2     dvswtd2
                  paicalc2 aicscr2  rdaiscr2 enerfat2 enersfa2 efatscr2 esftscr2
                  cholscr2 dqigpct2 dqigscr2 dqivpct2 dqivscr2 dqifpct2 dqifscr2
                  modspct2 modsscr2 modapct2 modascr2 modnas2  moddfs2  alcdrnk2
                  adrink2  ms2      dqi_r2   ;

do i=1 to 52; 
 allday2[i]=allday1[i];
end;
run;

data csf96344(drop=i dvg1_1   dvg1_2   dvg1_3   dvg1_4   dvg1_5   dvg1_6   dvg1_7
                     dvv1_1   dvv1_2   dvv1_3   dvv1_4   dvv1_5   dvv1_6   dvv1_7
                     dvdm1_1  dvdm1_2  dvdm1_3  dvdm1_4  dvdm1_5  dvdm1_6  dvf1_1
                     dvf1_2   dvsg1    dvsv1    dvsdm1   dvsf1    dvs1     dvswtd1
                     paicalc1 aicscr1  rdaiscr1 enerfat1 enersfa1 efatscr1 esftscr1
                     cholscr1 dqigpct1 dqigscr1 dqivpct1 dqivscr1 dqifpct1 dqifscr1
                     modspct1 modsscr1 modapct1 modascr1 modnas1  moddfs1  alcdrnk1
                     adrink1  ms1      dqi_r1   );
 set csf96344;

array allday1[52] dvg1_1   dvg1_2   dvg1_3   dvg1_4   dvg1_5   dvg1_6   dvg1_7
                  dvv1_1   dvv1_2   dvv1_3   dvv1_4   dvv1_5   dvv1_6   dvv1_7
                  dvdm1_1  dvdm1_2  dvdm1_3  dvdm1_4  dvdm1_5  dvdm1_6  dvf1_1
                  dvf1_2   dvsg1    dvsv1    dvsdm1   dvsf1    dvs1     dvswtd1
                  paicalc1 aicscr1  rdaiscr1 enerfat1 enersfa1 efatscr1 esftscr1
                  cholscr1 dqigpct1 dqigscr1 dqivpct1 dqivscr1 dqifpct1 dqifscr1
                  modspct1 modsscr1 modapct1 modascr1 modnas1  moddfs1  alcdrnk1
                  adrink1  ms1      dqi_r1   ;

array dayavg[52] d2dvg_1  d2dvg_2  d2dvg_3  d2dvg_4  d2dvg_5  d2dvg_6  d2dvg_7
                 d2dvv_1  d2dvv_2  d2dvv_3  d2dvv_4  d2dvv_5  d2dvv_6  d2dvv_7
                 d2dvdm_1 d2dvdm_2 d2dvdm_3 d2dvdm_4 d2dvdm_5 d2dvdm_6 d2dvf_1
                 d2dvf_2  d2dvsg   d2dvsv   d2dvsdm  d2dvsf   d2dvs    d2dvswtd
                 d2paical d2aicscr d2rdaisc d2enerfa d2enersf d2efatsc d2esftsc
                 d2cholsc d2dqigpc d2dqigsc d2dqivpc d2dqivsc d2dqifpc d2dqifsc
                 d2modspc d2modssc d2modapc d2modasc d2modnas d2moddfs d2alcdrn
                 d2adrink d2ms     d2dqi_r  ;

do i=1 to 52; 
 dayavg[i]=allday1[i];
end;
run;

**Merge data sets back into on obs per person;
data csf9634;
 merge csf96341
       csf96342
       csf96344;
 by hhid personid;

label

  /* Grains */
   dvg1_1   = "Day1 Tot: DV,Breads Whole Serv=>.5" 
   dvg1_2   = "Day1 Tot: DV,Breads Non-Whole Serv=>.5"
   dvg1_3   = "Day1 Tot: DV,Pasta Ser=>.5"
   dvg1_4   = "Day1 Tot: DV,Rice Ser=>.5"
   dvg1_5   = "Day1 Tot: DV,Cereal Whole Serv=>.5"
   dvg1_6   = "Day1 Tot: DV,Cereal Non-Whole Serv=>.5"
   dvg1_7   = "Day1 Tot: DV,Quick Breads Serv=>.5"

   dvg2_1   = "Day2 Tot: DV,Breads Whole Serv=>.5" 
   dvg2_2   = "Day2 Tot: DV,Breads Non-Whole Serv=>.5"
   dvg2_3   = "Day2 Tot: DV,Pasta Ser=>.5"
   dvg2_4   = "Day2 Tot: DV,Rice Ser=>.5"
   dvg2_5   = "Day2 Tot: DV,Cereal Whole Serv=>.5"
   dvg2_6   = "Day2 Tot: DV,Cereal Non-Whole Serv=>.5"
   dvg2_7   = "Day2 Tot: DV,Quick Breads Serv=>.5"

   d2dvg_1  = "2-Day Avg: DV,Breads Whole Serv=>.5"
   d2dvg_2  = "2-Day Avg: DV,Breads Non-Whole Serv=>.5"
   d2dvg_3  = "2-Day Avg: DV,Pasta Ser=>.5"
   d2dvg_4  = "2-Day Avg: DV,Rice Ser=>.5"
   d2dvg_5  = "2-Day Avg: DV,Cereal Whole Serv=>.5"
   d2dvg_6  = "2-Day Avg: DV,Cereal Non-Whole Serv=>.5"
   d2dvg_7  = "2-Day Avg: DV,Quick Breads Serv=>.5"

  /* Veges */
   dvv1_1   = "Day1 Tot: DV,Dark Green Veg. Serv=>.5" 
   dvv1_2   = "Day1 Tot: DV,Deep Yellow Veg. Serv=>.5" 
   dvv1_3   = "Day1 Tot: DV,Legume/Soy Veg. Serv=>.5" 
   dvv1_4   = "Day1 Tot: DV,Potato Veg. Serv=>.5" 
   dvv1_5   = "Day1 Tot: DV,Starchy Veg. Serv=>.5" 
   dvv1_6   = "Day1 Tot: DV,Tomato Veg. Serv=>.5" 
   dvv1_7   = "Day1 Tot: DV,Other Veg. Serv=>.5" 
   
   dvv2_1   = "Day2 Tot: DV,Dark Green Veg. Serv=>.5" 
   dvv2_2   = "Day2 Tot: DV,Deep Yellow Veg. Serv=>.5" 
   dvv2_3   = "Day2 Tot: DV,Legume/Soy Veg. Serv=>.5" 
   dvv2_4   = "Day2 Tot: DV,Potato Veg. Serv=>.5" 
   dvv2_5   = "Day2 Tot: DV,Starchy Veg. Serv=>.5" 
   dvv2_6   = "Day2 Tot: DV,Tomato Veg. Serv=>.5" 
   dvv2_7   = "Day2 Tot: DV,Other Veg. Serv=>.5" 
   
   d2dvv_1  = "2-Day Avg: DV,Dark Green Veg. Serv=>.5" 
   d2dvv_2  = "2-Day Avg: DV,Deep Yellow Veg. Serv=>.5" 
   d2dvv_3  = "2-Day Avg: DV,Legume/Soy Serv=>.5" 
   d2dvv_4  = "2-Day Avg: DV,Potato Serv=>.5" 
   d2dvv_5  = "2-Day Avg: DV,Starchy Serv=>.5" 
   d2dvv_6  = "2-Day Avg: DV,Tomato Serv=>.5" 
   d2dvv_7  = "2-Day Avg: DV,Other Vegetable Serv=>.5" 
_
  /* Dairy & Meat */
   dvdm1_1  = "Day1 Tot: DV,Milk & Yogurt Serv=>.5" 
   dvdm1_2  = "Day1 Tot: DV,Cheese Serv=>.5" 
   dvdm1_3  = "Day1 Tot: DV,Meat Organ Frank Serv=>.5" 
   dvdm1_4  = "Day1 Tot: DV,Poultry Serv=>.5" 
   dvdm1_5  = "Day1 Tot: DV,Fish Serv=>.5" 
   dvdm1_6  = "Day1 Tot: DV,Egg Serv=>.5" 
    
   dvdm2_1  = "Day2 Tot: DV,Milk & Yogurt Serv=>.5" 
   dvdm2_2  = "Day2 Tot: DV,Cheese Serv=>.5" 
   dvdm2_3  = "Day2 Tot: DV,Meat Organ Frank Serv=>.5" 
   dvdm2_4  = "Day2 Tot: DV,Poultry Serv=>.5" 
   dvdm2_5  = "Day2 Tot: DV,Fish Serv=>.5" 
   dvdm2_6  = "Day2 Tot: DV,Egg Serv=>.5" 
   
   d2dvdm_1 = "2-Day Avg: DV,Milk & Yogurt Serv=>.5" 
   d2dvdm_2 = "2-Day Avg: DV,Cheese Serv=>.5" 
   d2dvdm_3 = "2-Day Avg: DV,Meat Organ Frank Serv=>.5" 
   d2dvdm_4 = "2-Day Avg: DV,Poultry Serv=>.5" 
   d2dvdm_5 = "2-Day Avg: DV,Fish Serv=>.5" 
   d2dvdm_6 = "2-Day Avg: DV,Egg Serv=>.5" 

  /* Fruit */
   dvf1_1   = "Day1 Tot: DV,Cit. Mellon Berry Serv=>.5" 
   dvf1_2   = "Day1 Tot: DV,Other Fruit Serv=>.5" 
   
   dvf2_1   = "Day2 Tot: DV,Cit. Mellon Berry Serv=>.5" 
   dvf2_2   = "Day2 Tot: DV,Other Fruit Serv=>.5" 

   d2dvf_1  = "2-Day Avg: DV,Cit. Mellon Berry Serv=>.5" 
   d2dvf_2  = "2-Day Avg: DV,Other Fruit Serv=>.5" 
 
   paicalc1 = 'Day1 Total: AI % Calcium'
   paicalc2 = 'Day1 Total: AI % Calcium'
   d2paical = '2-Day Avg.: AI % Calcium'
   aicscr1  = 'Day1 Total: AI % Calcium 10pt score'
   aicscr2  = 'Day2 Total: AI % Calcium 10pt score'
   d2aicscr = '2-Day Avg.: AI % Calcium 10pt score'
   rdaiscr1 = 'Day1 Total: % RDA Iron 10pt score'
   rdaiscr2 = 'Day2 Total: % RDA Iron 10pt score'
   d2rdaisc = '2-Day Avg.: % RDA Iron 10pt score'
   enerfat1 = 'Day1 Tot: % energy from fat'
   enersfa1 = 'Day1 Tot: % energy from saturated fat'
   efatscr1 = 'Day1 Tot: 10pt Score of enerfat1'
   esftscr1 = 'Day1 Tot: 10pt Score of enersfa1'
   cholscr1 = 'Day1 Tot: cholesterol 10pt score'
   enerfat2 = 'Day2 Tot: % energy from fat'
   enersfa2 = 'Day2 Tot: % energy from saturated fat'
   efatscr2 = 'Day2 Tot: 10pt Score of enerfat2'
   esftscr2 = 'Day2 Tot: 10pt Score of enersfa2'
   cholscr2 = 'Day2 Tot: cholesterol 10pt score'
   d2enerfa = '2-Day Avg: % energy from fat'
   d2enersf = '2-Day Avg: % energy from saturated fat'
   d2efatsc = '2-Day Avg: 10pt Score of d2enerfa'
   d2esftsc = '2-Day Avg: 10pt Score of d2enersf'
   d2cholsc = '2-Day Avg: cholesterol 10pt score'

   dvs1     = "Day1 Tot Diet Variety Score (DVS)"
   dvs2     = "Day2 Tot Diet Variety Score (DVS)"
   d2dvs    = "2-Day Avg Diet Variety Score (DVS)"
   dvswtd1  = "Day1 Tot Diet Variety Scr(DVS),weighted"
   dvswtd2  = "Day2 Tot Diet Variety Scr(DVS),weighted"
   d2dvswtd = "2-Day Avg Diet Variety Scr(DVS),weighted"
   dvsg1    = "Day1 Tot Diet Variety Score (DVS)-Grain"
   dvsv1    = "Day1 Tot Diet Variety Score (DVS)-Veges"
   dvsdm1   = 'Day1 Tot Diet Varty Scr (DVS)-Dairy&Meat'
   dvsf1    = "Day1 Tot Diet Variety Score (DVS)-Fruit"
   dvsg2    = "Day2 Tot Diet Variety Score (DVS)-Grain"
   dvsv2    = "Day2 Tot Diet Variety Score (DVS)-Veges"
   dvsdm2   = 'Day2 Tot Diet Varty Scr (DVS)-Dairy&Meat'
   dvsf2    = "Day2 Tot Diet Variety Score (DVS)-Fruit"
   d2dvsg   = "2-Day Avg Diet Variety Score (DVS)-Grain"
   d2dvsv   = "2-Day Avg Diet Variety Score (DVS)-Veges"
   d2dvsdm  = '2-Day Avg Diet Varty Scr(DVS)-Dairy&Meat'
   d2dvsf   = "2-Day Avg Diet Variety Score (DVS)-Fruit"
   alcdrnk1 = "Day1 Tot: Alcohol Drinker"
   alcdrnk2 = "Day2 Tot: Alcohol Drinker"
   d2alcdrn = "2-Day Avg: Alcohol Drinker"
   adrink1  = "Day1: Tot alc drinks of alcohol drinker"
   adrink2  = "Day2: Tot alc drinks of alcohol drinker"
   d2adrink = "2-Day: Avg alc drinks of alcohol drinker"
  
   modascr1 = "Day1: Moderation of Alcohol Score"
   modnas1  = "Day1: Moderation of Sodium Score"
   modsscr1 = "Day1: Moderation of Added Sugar Score"
  
   modascr2 = "Day2: Moderation of Alcohol Score"
   modnas2  = "Day2: Moderation of Sodium Score"
   modsscr2 = "Day2: Moderation of Added Sugar Score"

   d2modasc = "2-Day Avg: Moderation of Alcohol Score"
   d2modnas = "2-Day Avg: Moderation of Sodium Score"
   d2modssc = "2-Day Avg: Mod. of Added Sugar Score"

   dqigpct1 = "Day1 Total: DQI Tot Grains % of Min"
   dqifpct1 = "Day1 Total: DQI Total Fruit % of Min"
   dqivpct1 = 'Day1 Total: DQI Total Veges&Soy % of Min'
   modapct1 = "Day1 Total: Mod of Alcohol % of Min"
   modspct1 = "Day1 Total: Mod of Added Sugar % of Min"
   dqigscr1 = "Day1 Total: DQI Total Grains 10pt score"
   dqifscr1 = "Day1 Total: DQI Total Fruit 10pt score"
   dqivscr1 = 'Day1 Total: DQI Tot Veges&Soy 10pt scr'

   dqigpct2 = "Day2 Total: DQI Tot Grains % of Min"
   dqifpct2 = "Day2 Total: DQI Total Fruit % of Min"
   dqivpct2 = 'Day2 Total: DQI Total Veges&Soy % of Min'
   modapct2 = "Day2 Total: Mod of Alcohol % of Min"
   modspct2 = "Day2 Total: Mod of Added Sugar % of Min"
   dqigscr2 = "Day2 Total: DQI Total Grains 10pt score"
   dqifscr2 = "Day2 Total: DQI Total Fruit 10pt score"
   dqivscr2 = 'Day2 Total: DQI Tot Veges&Soy 10pt scr'
 
   d2dqigpc = "2-Day Avg: DQI Tot Grains % of Min"
   d2dqifpc = "2-Day Avg: DQI Total Fruit % of Min"
   d2dqivpc = '2-Day Avg: DQI Total Veges&Soy % of Min'
   d2modapc = "2-Day Avg: Mod of Alcohol % of Min"
   d2modspc = "2-Day Avg: Mod of Added Sugar % of Min"
   d2dqigsc = "2-Day Avg: DQI Total Grains 10pt score"
   d2dqifsc = "2-Day Avg: DQI Total Fruit 10pt score"
   d2dqivsc = '2-Day Avg: DQI Tot Veges&Soy 10pt scr'

   moddfs1  = "Day1 Total: Moderation of Disc fat Score"
   moddfs2  = "Day2 Total: Moderation of Disc fat Score"
   d2moddfs = "2-Day Avg : Moderation of Disc fat Score"
   ms1      = "Day1 Total: Moderation Score(10pt score)"
   ms2      = "Day2 Total: Moderation Score(10pt score)"
   d2ms     = "2-Day Avg : Moderation Score(10pt score)"

   dqi_r1  = "Day1 Tot: DQI-Revised (100pt score)"
   dqi_r2  = "Day2 Tot:DQI-Revised (100pt score)"
   d2dqi_r = "2-Day Avg:DQI-Revised (100pt score)"
  ;;;
run;

** Save as a sorted data set ***;
proc sort data=csf9634 out=data.csf9634; by hhid personid;
run;

proc means data=data.csf9634;
run;
proc contents data=data.csf9634;
run;

endsas;

** checks that were done to check output;

proc means data=data.csf9634;
where age>=18; 
title2 "where age>=18, compare with previous version of this program's output";
run;

options &land1;
proc print data=data.csf9634; 
where dayct=1 and (comp_d1=1 and comp_d2=1);
title3 "People who Fasted on either Day1 or Day2";
title4 "Day1 fasters should be only people with no day1 total";
run;

title3 ; ** clears title3 and title4;
options &por;
proc print data=notfound noobs;
var foodcode fooddesc;
where graingrp=0 and (58000000<=foodcode<59000000);
title3 'Foodcode in the 58s that were not grouped by this program';
run;

proc sort data=csf9602 (keep=foodcode fooddesc graingrp) nodupkey; by foodcode;
run;

proc print data=csf9602 noobs;
 var foodcode graingrp fooddesc;
where graingrp>0;
title2 'Foodcodes in csf9602 where graingrp>0';
run;