BookmarkSubscribeRSS Feed
chester2018
Fluorite | Level 6

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.

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;

The attached data portion is something I don't want. The highlighted values in the min_week columb need to be missingsas.jpg

3 REPLIES 3
andreas_lds
Jade | Level 19

 

data exercise.writeinsMETS;
	set exercise.writeinsMETS;

 You should not overwrite the source dataset during development, an i would not do this even after the code has been tested.

 

if METS="Vigorous" or METS = "Moderate";
else if writein38_min_week=((v5qa38a*v5qa38b*7)/30);

A subsetting-if has no else part, so this will give you a syntax error.

 

To make it easier to understand the task, please post data in usable form an show the expected result.

I don't see the need for that many if-statements, but maybe i don't understand what you are trying to do.

fja
Lapis Lazuli | Level 10 fja
Lapis Lazuli | Level 10

Hello!

If you do not write anything in case of MET value = .  to the output dataset, then you could use:

if <condition> then delete;

to continue with the next line. Hope I understood you correctly.

--FJa

ChrisHemedinger
Community Manager

For anyone getting started with the nuances of the IF-THEN-ELSE statements in the SAS DATA step, I recommend this short tutorial by @brzcol

 

SAS Hackathon registration is open! Build your skills. Make connections. Enjoy creative freedom. Maybe change the world.

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!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 471 views
  • 0 likes
  • 4 in conversation