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

I am having an issue when using if-then-else in an array.

 

This is the code I want to use, but when it runs it seems to ignore the original if statement as you can see in the image there are individuals who have "Achieved" selected and the AchievedGoal variable is listed as "No".

 

data DSMESY2_test;
set DSMESY2;
array Ace ProblemSolving ReducingRisk BeingActive HealthyCoping HealthyEating Monitoring TakingMeds;
do over Ace;
if Ace = "Achieved" then AchievedGoal="Yes";
else AchievedGoal="No";
end;
run;
SAS Image 1.png
 
When I removed the "Else AchievedGoal="No"; statement, the array seems to work properly though.
data DSMESY2_test;
set DSMESY2;
array Ace ProblemSolving ReducingRisk BeingActive HealthyCoping HealthyEating Monitoring TakingMeds;
do over Ace;
if Ace = "Achieved" then AchievedGoal="Yes";
end;
run;
SAS Image 2.png
I have tried to trouble shoot this so many times and I can't figure out what is going on. I can do a work around for my reporting by doing a proc freq/missing, for the second code, but it is driving me crazy not knowing why the top code isn't working especially because I know I have used it with other datasets with now issue. 
1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Another option would be to avoid the DO loop and just use the IN operator:

data DSMESY2_test;
set DSMESY2;
array Ace ProblemSolving ReducingRisk BeingActive HealthyCoping HealthyEating Monitoring TakingMeds;
if "Achieved" in Ace then AchievedGoal="Yes";
else AchievedGoal="No";
run;

Personally, I would prefer a numeric 0-1 coded variable AchievedGoal, which would also avoid the IF-THEN/ELSE statement:

AchievedGoal="Achieved" in Ace;

A format with labels "Yes", "No" could be attached if needed.

 

View solution in original post

5 REPLIES 5
Kurt_Bremser
Super User

The way you write your code, only the evaluation of the last array element will make it through.

Restructure your code:

AchievedGoal = "No";
do over Ace;
  if Ace = "Achieved" then AchievedGoal = "Yes";
end;

 

Tom
Super User Tom
Super User

You were almost there.

The pattern you want is something like:

AchievedGoal="No ";
do over Ace;
  if Ace = "Achieved" then AchievedGoal="Yes";
end;

You set the result to false before the loop.  Then in the loop set it to true when the condition is met.

aberkowitz
Calcite | Level 5
Thank you! That makes so much sense.
FreelanceReinh
Jade | Level 19

Another option would be to avoid the DO loop and just use the IN operator:

data DSMESY2_test;
set DSMESY2;
array Ace ProblemSolving ReducingRisk BeingActive HealthyCoping HealthyEating Monitoring TakingMeds;
if "Achieved" in Ace then AchievedGoal="Yes";
else AchievedGoal="No";
run;

Personally, I would prefer a numeric 0-1 coded variable AchievedGoal, which would also avoid the IF-THEN/ELSE statement:

AchievedGoal="Achieved" in Ace;

A format with labels "Yes", "No" could be attached if needed.

 

Tom
Super User Tom
Super User

Another way to deal with this is to use an iterative DO loop (instead of using the DO OVER syntax that SAS has been trying to eliminate).  Then you can take advantage of the fact you can combine the iterative DO with an UNTIL() clause.

data DSMESY2_test;
  set DSMESY2;
  array Ace ProblemSolving ReducingRisk BeingActive HealthyCoping 
            HealthyEating Monitoring TakingMeds
  ;
  do index=1 to dim(Ace) until (AchievedGoal);
    AchievedGoal = (Ace[index]="Achieved");
  end;
  drop index;
run;

 

hackathon24-white-horiz.png

Join the 2025 SAS Hackathon!

Calling all data scientists and open-source enthusiasts! Want to solve real problems that impact your company or the world? Register to hack by August 31st!

Register Now

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 309 views
  • 4 likes
  • 4 in conversation