BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
chester2018
Obsidian | Level 7

Hi! I'm not sure what's wrong with my SAS code! I'm trying to score the MEPA (Mediterranean Eating Pattern for Americans), but my calculations aren't adding up! I'll show some examples!

 

For Cheese, if a participant eats more than 4 servings of cheese a day/week, then they have a 0. If they have 4 or less, then they have a 1. And butter and nuts have a similar scoring.

data MEPA.scores;
	set MEPA.scores;
	if (cheese_dy gt 4) then cheese_score =0;
	else if (cheese_dy le 4) then cheese_score = 1;
	else if (cheese_dy=0 and cheese_wk le 4) then cheese_score = 1;
	else if (cheese_dy=0 and cheese_wk gt 4) then cheese_score = 0;
	else if (cheese_dy=. and cheese_wk=.) then cheese_score = 0;
run;

data MEPA.scores;
	set MEPA.scores;
	if (butter_dy > 5) then butter_score =0;
	else if (butter_dy le 5) then butter_score = 1;
	else if (butter_dy=0 and butter_wk le 5) then butter_score = 1;
	else if (butter_dy=0 and butter_wk > 5) then butter_score = 0;
	else if (butter_dy=. and butter_wk=.) then butter_score = 0;
run;

data  MEPA.scores;
	set  MEPA.scores;
	if (nuts_dy ge 4) then nuts_score =1;
	else if (nuts_dy < 4) then nuts_score = 0;
	else if (nuts_dy=0 and nuts_wk < 4) then nuts_score = 0;
	else if (nuts_dy=0 and nuts_wk ge 4) then nuts_score = 1;
	else if (nuts_dy=. and nuts_wk=.) then nuts_score = 0;
run;

For some reason when I score my questionnaire, these variables are the ones that aren't getting the proper score. One participant indicated they consumed 5 servings of nuts_wk, and yet their nuts_score was calculated as 0.

 

Hopefully my explanation and the questionnaire makes sense!

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

First a very serious warning where you may have damaged your data in an earlier test or attempt:

Use of code with the output data set the same as the input means the OUTPUT version completely replaces input.

 

data somename;
    set somename;

So a logic error or just running the same code repeatedly that modifies an existing variable can result is multiple changes and unexpected results.

 

Instead of reusing the same data set create a new one from the input.

ADD code the data step to avoid creating multiple unneeded datasets.

 

Run this code an share the results. It may show you where you have problems easily:

 

Proc freq data=mepa.scores;
   tables cheese_dy*cheese_wk*cheese_score
             butter_dy*butter_wk*butter_score
             nuts_dy*nuts_wk*nuts_score
            / list missing;
run;

 

 

You do have a problem with the LT or LE for the _dy variables. Missing is always less than anything.

So the following is true when Cheese_dy is missing.

 if (cheese_dy le 4)

 You would need to make these something like the following to handle missing correctly:

 if (0 le cheese_dy le 4)    

 

View solution in original post

2 REPLIES 2
ballardw
Super User

First a very serious warning where you may have damaged your data in an earlier test or attempt:

Use of code with the output data set the same as the input means the OUTPUT version completely replaces input.

 

data somename;
    set somename;

So a logic error or just running the same code repeatedly that modifies an existing variable can result is multiple changes and unexpected results.

 

Instead of reusing the same data set create a new one from the input.

ADD code the data step to avoid creating multiple unneeded datasets.

 

Run this code an share the results. It may show you where you have problems easily:

 

Proc freq data=mepa.scores;
   tables cheese_dy*cheese_wk*cheese_score
             butter_dy*butter_wk*butter_score
             nuts_dy*nuts_wk*nuts_score
            / list missing;
run;

 

 

You do have a problem with the LT or LE for the _dy variables. Missing is always less than anything.

So the following is true when Cheese_dy is missing.

 if (cheese_dy le 4)

 You would need to make these something like the following to handle missing correctly:

 if (0 le cheese_dy le 4)    

 

chester2018
Obsidian | Level 7

Thank you so much! 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 351 views
  • 1 like
  • 2 in conversation