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

## Create Variables Based on Previous Variable

Hi everyone!

I'm creating a new variable, and I'm having issues with the IF-THEN-ELSE statements. For my variable, I would like to calculate the weekly minutes only if that participant has a MET value (either Moderate or Vigorous). Therefore, people without a MET value won't get the calculation.

So, participants may or may not have a MET value (indicated by Vigorous or Moderate). If they do have a MET value, I would like to calculate their average weekly activity (min_week = ((a*b*7)/30)). So, if someone with a MET value =., I don't want to give them an average weekly activity. The attached data is what I don't want. I don't want the highlighted portions because the participants don't have a METS value.

Hope I made some sense

data exercise.writeinsMETS;
set exercise.writeinsMETS;
if METS="Vigorous" or METS = "Moderate";
else if writein38_min_week=((v5qa38a*v5qa38b*7)/30);
else if writein39_min_week=((v5qa39a*v5qa39b*7)/30);
else if writein40_min_week=((v5qa40a*v5qa40b*7)/30);
else max(writein38_min_week, writein39_min_week,writein40_min_week) = .;
run;

1 ACCEPTED SOLUTION

Accepted Solutions
Diamond | Level 26

## Re: Create Variables Based on Previous Variable

Here is your IF statement in your own words:

If they do have a MET value, I would like to calculate their average weekly activity (min_week = ((a*b*7)/30)). So, if someone with a MET value =., I don't want to give them an average weekly activity.

What you have in your code bears little resemblance to the above.

To put this into something close to SAS code, this is what you want, where I leave blanks for you to fill in as a homework assignment,

if not missing(_________) then min_week=_____________; else min_week=________;

Explanation

if not missing(________) /* you said if they do have a MET value */
then min_week=_________;  /* you said calculate their average weekly activity */
else min_week=__________; /* you said you don't want them to have an average weekly activity */

By the way, it isn't really helpful to have variables in your code that are not mentioned in your explanation, we don't know what they and we don't know how to use them properly.

--
Paige Miller
2 REPLIES 2
Diamond | Level 26

## Re: Create Variables Based on Previous Variable

Here is your IF statement in your own words:

If they do have a MET value, I would like to calculate their average weekly activity (min_week = ((a*b*7)/30)). So, if someone with a MET value =., I don't want to give them an average weekly activity.

What you have in your code bears little resemblance to the above.

To put this into something close to SAS code, this is what you want, where I leave blanks for you to fill in as a homework assignment,

if not missing(_________) then min_week=_____________; else min_week=________;

Explanation

if not missing(________) /* you said if they do have a MET value */
then min_week=_________;  /* you said calculate their average weekly activity */
else min_week=__________; /* you said you don't want them to have an average weekly activity */

By the way, it isn't really helpful to have variables in your code that are not mentioned in your explanation, we don't know what they and we don't know how to use them properly.

--
Paige Miller
Obsidian | Level 7

## Re: Create Variables Based on Previous Variable

Thank you so much! I didn't realize my code and variables were not correctly presenteded!

Discussion stats
• 2 replies
• 473 views
• 0 likes
• 2 in conversation