Hi! I'm assigning category names from multiple variables (moderate vs vigorous activities). The problem I'm having is that the category "Moderate" won't appear in my dataset, but "Vigorous" will. I highlighted the issue by pointing out that values that are lower than 6 should have the name "Moderate". The values that are 6+ have "Vigorous", which is good. 
Attached is a screenshot of the dataset and the problem
data exercise.writeinactivities;
set exercise.writeinactivities;
length METS $10;
if METS_38=. then METS="";
else if METS_38 > = 6.0 then METS="Vigorous";
else if METS_39=. then METS="";
else if METS_39 > = 6.0 then METS="Vigorous";
else if METS_40=. then METS="";
else if METS_40 > = 6.0 then METS="Vigorous";
else METS="Moderate";
run;
Lets follow your code logic for row 2 where Mets_38 is 4
if METS_38=. then METS=""; 4 is not missing so this is false and the next branch executes.
else if METS_38 > = 6.0 then METS="Vigorous"; 4 is not greater than 6 so the if is false and the next branch executes.
else if METS_39=. then METS=""; On row 2 Mets_39 is missing so Mets is set to "" and the comparisons stop.
You either need to insert an additional "else if mets_38 < 6 then Mets='Moderate'; (and in other places)
or provide a complete set of RULES for the assignment of Mets. Obviously the code is not doing what you want so that is not a sufficient description of the rules.
Did you intend to set the value based on the largest of the Mets_38, Mets_39 and Mets_40 values? The smallest? The Average? Or only consider Mets_39 when Mets_38 is smaller than 39? Or some such rule.
If you want to use the the largest of the three to set the value then use something like:
data exercise.writeinactivities; set exercise.writeinactivities; length METS $10; if missing(max(METS_38,Mets_39,Mets_40)) then METS=""; else if max(METS_38,Mets_39,Mets_40) ge 6.0 then METS="Vigorous"; else METS="Moderate"; run;
Caution: Habitual use of the output set with the same name as the input set may cause problems with minor logic issues as you completely replace the input set when you do this:
data exercise.writeinactivities; set exercise.writeinactivities;
Better to use a new output set name.
Why isn't row 9 highlighted?
How about this:
data want;
length mets $ 10;
set exercise.writeinactivities;
maxx=max(of mets_38-mets_40);
if maxx>=6 then mets='Vigorous';
else if maxx>0 then mets='Moderate';
run;
Your code fails because of this:
else if METS_39=. then METS="";
When METS_39=. then you never get to the rest of the ELSE statements, and so the last one where you assign the value "Moderate".
Lets follow your code logic for row 2 where Mets_38 is 4
if METS_38=. then METS=""; 4 is not missing so this is false and the next branch executes.
else if METS_38 > = 6.0 then METS="Vigorous"; 4 is not greater than 6 so the if is false and the next branch executes.
else if METS_39=. then METS=""; On row 2 Mets_39 is missing so Mets is set to "" and the comparisons stop.
You either need to insert an additional "else if mets_38 < 6 then Mets='Moderate'; (and in other places)
or provide a complete set of RULES for the assignment of Mets. Obviously the code is not doing what you want so that is not a sufficient description of the rules.
Did you intend to set the value based on the largest of the Mets_38, Mets_39 and Mets_40 values? The smallest? The Average? Or only consider Mets_39 when Mets_38 is smaller than 39? Or some such rule.
If you want to use the the largest of the three to set the value then use something like:
data exercise.writeinactivities; set exercise.writeinactivities; length METS $10; if missing(max(METS_38,Mets_39,Mets_40)) then METS=""; else if max(METS_38,Mets_39,Mets_40) ge 6.0 then METS="Vigorous"; else METS="Moderate"; run;
Caution: Habitual use of the output set with the same name as the input set may cause problems with minor logic issues as you completely replace the input set when you do this:
data exercise.writeinactivities; set exercise.writeinactivities;
Better to use a new output set name.
Thanks!!!
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!Select SAS Training centers are offering in-person courses. View upcoming courses for: