SAS Macro Truncating IDs

纵然是瞬间 提交于 2020-03-25 18:41:46

问题


I am using the ASA24 HEI SAS macro for the SAS University Edition.

I believe SAS is truncating my IDs (ASA24 Usernames). In my data (.csv) I have Usernames like

After the macro code, the GFHSp040x (x is a natural number) IDs get truncated to GFHSp040. Is this related to how the macro imports?

Code below:

 /*The SAS program (HEI-2015 Individual Scores using multiple days of data from ASA24-2016 and ASA24-2018)
                                                     ByPerson.SAS */                                              

/*This SAS program can be used to calculate Healthy Eating Index (HEI)-2015 scores from 24-hour recall or food records data collected using ASA24-2016 and ASA24-2018. This program calculates HEI-2015 component and total scores for each individual (using multiple recalls, if available, for a single respondent). Additional code that calculates HEI-2015 component and total scores for each day of 24HR     recall or food record data is available on the ASA24 HEI Resources page. */                   

/*This program has been tested using SAS UNIVERSITY EDITION. */                                      
/*Note: Some users have found that the SAS program will drop observations 
from the analysis if the ID field is not the same length 
for all observations.  To prevent this error, the observations with the 
longest ID length should be listed first when the data is imported into SAS. */

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

%let home = /folders/myfolders/ASA24; 

/* Rename FS_BL_CH_02.02.20_Totals to specify your input file. Do not forget .csv at the end of the name. */

filename Totals "&home/Totals/FS_BL_PA_02.02.20_Totals.csv"; /* In this example, the ASA24-2016 or ASA24-2018 Daily Total Nutrient and Pyramid Equivalents data “Totals”, are saved in a folder called “Totals”, within the “home” folder. The data are in csv format. */

/* Rename FS_BL_CH_02.02.20_HEI to specify your output file. */

filename res "&home/FS_BL_PA_02.02.20_HEI"; 

/*NOTE: Once you have renamed the above, all you need to do is run the SAS program.*/

%include "&home/hei2015.score.macro.sas";

TITLE 'ASA24-2016 and ASA24-2018 HEI-2015 scores - by person using all days';


/*Step 1.
Input daily total data and create five additional required variables.  These variables are:  
FWHOLEFRT, MONOPOLY, VTOTALLEG, VDRKGRLEG, PFALLPROTLEG, and PFSEAPLANTLEG
*/


Proc import datafile=Totals
  Out=Totals
  Dbms=csv
  Replace;
  Getnames=yes;
Run;



DATA Totals;
  SET Totals;  

  FWHOLEFRT=F_CITMLB+F_OTHER;

  MONOPOLY=MFAT+PFAT;

  VTOTALLEG=V_TOTAL+V_LEGUMES;
  VDRKGRLEG=V_DRKGR+V_LEGUMES;

  PFALLPROTLEG=PF_MPS_TOTAL+PF_EGGS+PF_NUTSDS+PF_SOY+PF_LEGUMES; 
  PFSEAPLANTLEG=PF_SEAFD_HI+PF_SEAFD_LOW+PF_NUTSDS+PF_SOY+PF_LEGUMES;

run; 





/*Step 2.
Calculates total food group and nutrient intake over all possible days reported per individual.
*/

proc sort data=Totals;
  by UserName UserID;
run;

*get sum per person of variables of interest;
proc means data=Totals noprint;
  by UserName UserID;
  var KCAL VTOTALLEG VDRKGRLEG F_TOTAL FWHOLEFRT G_WHOLE D_TOTAL 
      PFALLPROTLEG PFSEAPLANTLEG MONOPOLY SFAT SODI G_REFINED ADD_SUGARS;
  output out=idtot sum=;
run;


/*Step 3. 
 Runs the HEI2015 scoring macro which calculates intake density amounts and HEI scores.
*/

%HEI2015 (indat=idtot,
          kcal= KCAL,
      vtotalleg= VTOTALLEG,
      vdrkgrleg= VDRKGRLEG,
      f_total= F_TOTAL,
      fwholefrt=FWHOLEFRT,
      g_whole= G_WHOLE,
      d_total= D_TOTAL,
          pfallprotleg= PFALLPROTLEG,
      pfseaplantleg= PFSEAPLANTLEG,
      monopoly=MONOPOLY,
      satfat=SFAT,
      sodium=SODI,
      g_refined=G_REFINED,
      add_sugars=ADD_SUGARS,
      outdat=hei2015);


/*Step 4. 
 Displays and saves the results.
*/ 



Data hei2015r (keep=UserName UserID kcal HEI2015C1_TOTALVEG HEI2015C2_GREEN_AND_BEAN HEI2015C3_TOTALFRUIT
      HEI2015C4_WHOLEFRUIT HEI2015C5_WHOLEGRAIN HEI2015C6_TOTALDAIRY HEI2015C7_TOTPROT HEI2015C8_SEAPLANT_PROT 
      HEI2015C9_FATTYACID HEI2015C10_SODIUM HEI2015C11_REFINEDGRAIN HEI2015C12_SFAT HEI2015C13_ADDSUG HEI2015_TOTAL_SCORE);
  Set hei2015;
  Run;

proc means n nmiss min max mean data=hei2015r;
run;

proc export data= hei2015r
  file=res
  dbms=xlsx
  replace;
run;

_______________________________ THE MACRO

/*************************************************************************/
/*************************************************************************/
/*                                                                       */
/*                 THE HEI-2015 SCORING MACRO                            */                                               
/*                  (hei2015.score.macro.sas)                            */
/*************************************************************************/
/*                     VERSION 1.0        06/25/2017                     */   
/*                                                                       */
/*                                                                       */
/* This HEI-2015 macro is to be used to calculate densities and          */
/* and HEI-2015 component and total scores.                              */
/*                                       */ 
/* The macro requires an input dataset with variables for each of        */
/* the HEI-2015 components, noted below.                                 */
/*                                       */ 
/* The resulting dataset, which is named by the user, contains the       */
/* same variables as the supplied dataset, and creates 27 new            */
/* variables. These include:                             */
/*                                                                       */                 
/*   The densities (per 1000 kcal) or percent (of total calories)        */
/*    for each of the 13 HEI-2015 components.                    */
/*                                                                       */
/*   Scores for the 13 components of the HEI-2015.                       */
/*                                                                       */
/*   The total HEI-2015 score, which is the sum of the                   */
/*    scores for the 13 components.                                      */
/*                                                                       */                                                                  
/* The syntax for calling the macro is:                                  */
/*                                                                       */
/* %HEI 2015                                     */
/* (indat=,kcal=,vtotalleg=,vdrkgrleg=,f_total=,fwholefrt=,g_whole=      */
/*    d_total=,pfallprotleg=,pfseaplantleg=,monopoly=,satfat=,sodium=,   */
/*    g_refined=,add_sugars=,outdat=)                                    */
/*                                                                       */
/*  where                                                                */
/*                                                                       */
/*   "indat"        * Specifies the dataset to be used.                  */
/*                                                                       */
/*   "kcal"         * Specifies calorie amount.                          */
/*                                                                       */
/*   "vtotalleg"    * Specifies the intake of total veg plus             */
/*                      legumes in cup eq.                               */
/*                                                                       */
/*   "vdrkgrleg"    * Specifies the intake of dark green veg             */
/*                      plus legumes in cup eq.                          */
/*                                                                       */
/*   "f_total"      * Specifies the intake of total fruit in cup eq      */
/*                                                                       */
/*   "fwholefrt"    * Specifies the intake of whole fruit in cup eq.     */
/*                                                                       */
/*   "g_whole"      * Specifies the intake of whole grain in oz. eq.     */
/*                                                                       */
/*   "d_total"      * Specifies the intake of total dairy in cup eq.     */
/*                                                                       */
/*   "pfallprotleg" * Specifies the intake of total protein              */
/*                      (includes legumes) in oz. eq.                    */
/*                                                                       */
/*   "pfseaplantleg"  * Specifies the intake of seafood, fish and plant  */
/*                      protein (includes legumes) in oz. eq.            */
/*                                                                       */
/*   "monopoly"       * Specifies the grams of mono fat plus poly fat.   */
/*                                                                       */
/*   "satfat"         * Specifies the grams of saturated fat.            */
/*                                                                       */
/*   "sodium"         * Specifies the mg of sodium.                      */
/*                                                                       */                                                                 
/*   "g_refined"      * Specifies the intake of refined                  */
/*                       grain in oz. eq.                                */
/*                                                                       */
/*   "add_sugars"     * Specifies the intake of added sugars in tsp. eq. */
/*                                                                       */
/*   "outdat"         * Specifies the name of the resulting dataset.     */
/*                                       */
/*                                                                       */                                                                 
/* Caution:  variable names "FARMIN", "FARMAX", "SODMIN",                */
/*   "SODMAX", "RGMIN", "RGMAX", "SFATMIN", "SFATMAX", "ADDSUGMIN",      */
/*   "ADDSUGMAX" are reserved for this macro.                            */
/*                                                                       */
/*                                                                       */
/*************************************************************************/
;




%macro HEI2015 (indat=,kcal=,vtotalleg=,vdrkgrleg=,f_total=,fwholefrt=,g_whole=,d_total=,
  pfallprotleg=,pfseaplantleg=,monopoly=,satfat=,sodium=,g_refined=,add_sugars=,outdat=);

data &outdat (drop=FARMIN FARMAX SODMAX SODMIN RGMIN RGMAX SFATMIN SFATMAX ADDSUGMIN ADDSUGMAX);
  set &indat;

  IF &kcal > 0 then VEGDEN=&vtotalleg/(&kcal/1000);
  HEI2015C1_TOTALVEG=5*(VEGDEN/1.1);
  IF HEI2015C1_TOTALVEG > 5 THEN HEI2015C1_TOTALVEG=5;
  IF VEGDEN=0 THEN HEI2015C1_TOTALVEG=0;

  IF &kcal > 0 then GRBNDEN=&vdrkgrleg/(&kcal/1000);
  HEI2015C2_GREEN_AND_BEAN=5*(GRBNDEN/0.2);
  IF HEI2015C2_GREEN_AND_BEAN > 5 THEN HEI2015C2_GREEN_AND_BEAN=5;
  IF GRBNDEN=0 THEN HEI2015C2_GREEN_AND_BEAN=0;

  IF &kcal > 0 then FRTDEN=&f_total/(&kcal/1000);
  HEI2015C3_TOTALFRUIT=5*(FRTDEN/0.8);
  IF HEI2015C3_TOTALFRUIT > 5 THEN HEI2015C3_TOTALFRUIT=5;
  IF FRTDEN=0 THEN HEI2015C3_TOTALFRUIT=0;  

  IF &kcal > 0 then WHFRDEN=&fwholefrt/(&kcal/1000);
  HEI2015C4_WHOLEFRUIT=5*(WHFRDEN/0.4); 
  IF HEI2015C4_WHOLEFRUIT > 5 THEN HEI2015C4_WHOLEFRUIT=5;
  IF WHFRDEN=0 THEN HEI2015C4_WHOLEFRUIT=0; 

  IF &kcal > 0 then WGRNDEN=&g_whole/(&kcal/1000);
  HEI2015C5_WHOLEGRAIN=10*(WGRNDEN/1.5);
  IF HEI2015C5_WHOLEGRAIN > 10 THEN HEI2015C5_WHOLEGRAIN=10;
  IF WGRNDEN=0 THEN HEI2015C5_WHOLEGRAIN=0;

  IF &kcal > 0 then DAIRYDEN=&d_total/(&kcal/1000);
  HEI2015C6_TOTALDAIRY=10*(DAIRYDEN/1.3);
  IF HEI2015C6_TOTALDAIRY > 10 THEN HEI2015C6_TOTALDAIRY=10;
  IF DAIRYDEN=0 THEN HEI2015C6_TOTALDAIRY=0;

  IF &kcal > 0 then PROTDEN=&pfallprotleg/(&kcal/1000);
  HEI2015C7_TOTPROT=5*(PROTDEN/2.5);
  IF HEI2015C7_TOTPROT > 5 THEN HEI2015C7_TOTPROT=5;
  IF PROTDEN=0 THEN HEI2015C7_TOTPROT=0;

  IF &kcal > 0 then SEAPLDEN=&pfseaplantleg/(&kcal/1000);
  HEI2015C8_SEAPLANT_PROT=5*(SEAPLDEN/0.8);
  IF HEI2015C8_SEAPLANT_PROT > 5 THEN HEI2015C8_SEAPLANT_PROT=5;
  IF SEAPLDEN=0 THEN HEI2015C8_SEAPLANT_PROT=0;

  IF &satfat > 0 THEN FARATIO=&monopoly/&satfat;
  FARMIN=1.2;
  FARMAX=2.5;
  if &satfat=0 and &monopoly=0 then HEI2015C9_FATTYACID=0;
    else if &satfat=0 and &monopoly > 0 then HEI2015C9_FATTYACID=10;
    else if FARATIO >= FARMAX THEN HEI2015C9_FATTYACID=10;
    else if FARATIO <= FARMIN THEN HEI2015C9_FATTYACID=0;
    else HEI2015C9_FATTYACID=10* ( (FARATIO-FARMIN) / (FARMAX-FARMIN) );

  IF &kcal > 0 then SODDEN=&sodium/&kcal;
  SODMIN=1.1;
  SODMAX=2.0;
  IF SODDEN <= SODMIN THEN HEI2015C10_SODIUM=10;
    ELSE IF SODDEN >= SODMAX THEN HEI2015C10_SODIUM=0;
    ELSE HEI2015C10_SODIUM=10 - (10 * (SODDEN-SODMIN) / (SODMAX-SODMIN) );

  IF &kcal > 0 then RGDEN=&g_refined/(&kcal/1000);
  RGMIN=1.8;
  RGMAX=4.3;
  IF RGDEN <= RGMIN THEN HEI2015C11_REFINEDGRAIN=10;
    ELSE IF RGDEN >= RGMAX THEN HEI2015C11_REFINEDGRAIN=0;
    ELSE HEI2015C11_REFINEDGRAIN=10 - ( 10* (RGDEN-RGMIN) / (RGMAX-RGMIN) ); 

  IF &kcal > 0 then SFAT_PERC=100*(&satfat*9/&kcal); 
  SFATMIN=8;
  SFATMAX=16;
  IF SFAT_PERC >= SFATMAX THEN HEI2015C12_SFAT=0;
    ELSE IF SFAT_PERC <= SFATMIN THEN HEI2015C12_SFAT=10;
    ELSE HEI2015C12_SFAT= 10 - ( 10* (SFAT_PERC-SFATMIN) / (SFATMAX-SFATMIN) );

  IF &kcal > 0 then ADDSUG_PERC=100*(&add_sugars*16/&kcal); 
  ADDSUGMIN=6.5;
  ADDSUGMAX=26;
  IF ADDSUG_PERC >= ADDSUGMAX THEN HEI2015C13_ADDSUG=0;
    ELSE IF ADDSUG_PERC <= ADDSUGMIN THEN HEI2015C13_ADDSUG=10;
    ELSE HEI2015C13_ADDSUG= 10 - ( 10* (ADDSUG_PERC-ADDSUGMIN) / (ADDSUGMAX-ADDSUGMIN) );


IF &kcal=0 THEN DO;
  HEI2015C1_TOTALVEG=0; HEI2015C2_GREEN_AND_BEAN=0; HEI2015C3_TOTALFRUIT=0; HEI2015C4_WHOLEFRUIT=0; HEI2015C5_WHOLEGRAIN=0; HEI2015C6_TOTALDAIRY=0;
  HEI2015C7_TOTPROT=0;  HEI2015C8_SEAPLANT_PROT=0; HEI2015C9_FATTYACID=0; HEI2015C10_SODIUM=0; HEI2015C11_REFINEDGRAIN=0; HEI2015C12_SFAT=0; HEI2015C13_ADDSUG=0;
  END;

/**Calculate HEI-2015 total score**/
/*total HEI-2015 score is the sum of 13 HEI component scores*/

HEI2015_TOTAL_SCORE = HEI2015C1_TOTALVEG + HEI2015C2_GREEN_AND_BEAN + HEI2015C3_TOTALFRUIT + HEI2015C4_WHOLEFRUIT + HEI2015C5_WHOLEGRAIN + HEI2015C6_TOTALDAIRY +
  HEI2015C7_TOTPROT + HEI2015C8_SEAPLANT_PROT + HEI2015C9_FATTYACID + HEI2015C10_SODIUM + HEI2015C11_REFINEDGRAIN + HEI2015C12_SFAT + HEI2015C13_ADDSUG;


LABEL HEI2015_TOTAL_SCORE='TOTAL HEI-2015 SCORE'
      HEI2015C1_TOTALVEG='HEI-2015 COMPONENT 1 TOTAL VEGETABLES'
      HEI2015C2_GREEN_AND_BEAN='HEI-2015 COMPONENT 2 GREENS AND BEANS'
      HEI2015C3_TOTALFRUIT='HEI-2015 COMPONENT 3 TOTAL FRUIT'
      HEI2015C4_WHOLEFRUIT='HEI-2015 COMPONENT 4 WHOLE FRUIT'
      HEI2015C5_WHOLEGRAIN='HEI-2015 COMPONENT 5 WHOLE GRAINS'
      HEI2015C6_TOTALDAIRY='HEI-2015 COMPONENT 6 DAIRY'
      HEI2015C7_TOTPROT='HEI-2015 COMPONENT 7 TOTAL PROTEIN FOODS'

      HEI2015C8_SEAPLANT_PROT='HEI-2015 COMPONENT 8 SEAFOOD AND PLANT PROTEIN'
      HEI2015C9_FATTYACID='HEI-2015 COMPONENT 9 FATTY ACID RATIO'
      HEI2015C10_SODIUM='HEI-2015 COMPONENT 10 SODIUM'
      HEI2015C11_REFINEDGRAIN='HEI-2015 COMPONENT 11 REFINED GRAINS'
      HEI2015C12_SFAT='HEI-2015 COMPONENT 12 SAT FAT'
      HEI2015C13_ADDSUG='HEI-2015 COMPONENT 13 ADDED SUGAR'
      VEGDEN='DENSITY OF TOTAL VEGETABLES PER 1000 KCAL'
      GRBNDEN='DENSITY OF DARK GREEN VEG AND BEANS PER 1000 KCAL'
      FRTDEN='DENSITY OF TOTAL FRUIT PER 1000 KCAL'
      WHFRDEN='DENSITY OF WHOLE FRUIT PER 1000 KCAL'
      WGRNDEN='DENSITY OF WHOLE GRAIN PER 1000 KCAL'
      DAIRYDEN='DENSITY OF DAIRY PER 1000 KCAL'
      PROTDEN='DENSITY OF TOTAL PROTEIN PER 1000 KCAL'
      SEAPLDEN='DENSITY OF SEAFOOD AND PLANT PROTEIN PER 1000 KCAL'
      FARATIO='FATTY ACID RATIO'
      SODDEN='DENSITY OF SODIUM PER 1000 KCAL'
      RGDEN='DENSITY OF REFINED GRAINS PER 1000 KCAL'
      SFAT_PERC='PERCENT OF CALORIES FROM SAT FAT'
      ADDSUG_PERC='PERCENT OF CALORIES FROM ADDED SUGAR'
      ;

run;


%mend HEI2015;




/*  END OF THE HEI2015 MACRO                                       */
/*******************************************************************/

回答1:


So the macro is NOT calling PROC IMPORT. Your code is doing that. You can add the GUESSINGROWS=MAX statement to the PROC IMPORT step and it will do a better job of guessing how long to define the character variables.

Or just write your own data step to read the CSV file. You can see the data step that PROC IMPORT generated in your log. You can use that as a model if you want.



来源:https://stackoverflow.com/questions/60509891/sas-macro-truncating-ids

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!