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

I wrote the code below to consolidate mutually-exclusive variables into 2 "catch-all" variables, but the only line that's copying correctly is the one outside of the DO loop. What am I missing? I appreciate any and all help - thanks. 

 

IF Q_4_1_1 NE . THEN DO;
ChatGPT41 = Q_4_1_1; ChatGPT42 = Q_4_1_2; ChatGPT43 = Q_4_1_3; ChatGPT5 = Q_5_1; ChatGPT6 = Q_6_1; ChatGPT7 = Q_7_1;
END;
IF Q_4_2_1 NE . THEN DO;

ChatGPT41 = Q_4_2_1; ChatGPT42 = Q_4_2_2; ChatGPT43 = Q_4_2_3; ChatGPT5 = Q_5_2; ChatGPT6 = Q_6_2; ChatGPT7 = Q_7_2;
END;
IF Q_4_3_1 NE . THEN DO;
ChatGPT41 = Q_4_3_1; ChatGPT42 = Q_4_3_2; ChatGPT43 = Q_4_3_3; ChatGPT5 = Q_5_3; ChatGPT6 = Q_6_3; ChatGPT7 = Q_7_3;
END;
IF Q_4_4_1 NE . THEN DO;
ChatGPT41 = Q_4_4_1; ChatGPT42 = Q_4_4_2; ChatGPT43 = Q_4_4_3; ChatGPT5 = Q_5_4; ChatGPT6 = Q_6_4; ChatGPT7 = Q_7_4;
END;
ELSE ChatGPT41 = Q_4_5_1; ChatGPT42 = Q_4_5_2; ChatGPT43 = Q_4_5_3; ChatGPT5 = Q_5_5; ChatGPT6 = Q_6_5; ChatGPT7 = Q_7_5;

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

As a minimum on suspects that your ELSE is improperly structured.

First, restructure the code for a certain amount of legibility:

IF Q_4_1_1 NE . THEN DO;
   ChatGPT41 = Q_4_1_1; 
   ChatGPT42 = Q_4_1_2; 
   ChatGPT43 = Q_4_1_3; 
   ChatGPT5 = Q_5_1; 
   ChatGPT6 = Q_6_1; 
   ChatGPT7 = Q_7_1;
END;

IF Q_4_2_1 NE . THEN DO;
   ChatGPT41 = Q_4_2_1; 
   ChatGPT42 = Q_4_2_2; 
   ChatGPT43 = Q_4_2_3; 
   ChatGPT5 = Q_5_2; 
   ChatGPT6 = Q_6_2; 
   ChatGPT7 = Q_7_2;
END;
IF Q_4_3_1 NE . THEN DO;
   ChatGPT41 = Q_4_3_1; 
   ChatGPT42 = Q_4_3_2; 
   ChatGPT43 = Q_4_3_3; 
   ChatGPT5 = Q_5_3; 
   ChatGPT6 = Q_6_3; 
   ChatGPT7 = Q_7_3;
END;
IF Q_4_4_1 NE . THEN DO;
   ChatGPT41 = Q_4_4_1; 
   ChatGPT42 = Q_4_4_2; 
   ChatGPT43 = Q_4_4_3; 
   ChatGPT5 = Q_5_4; 
   ChatGPT6 = Q_6_4; 
   ChatGPT7 = Q_7_4;
END;
ELSE ChatGPT41 = Q_4_5_1; 
ChatGPT42 = Q_4_5_2; 
ChatGPT43 = Q_4_5_3; 
ChatGPT5 = Q_5_5; 
ChatGPT6 = Q_6_5; 
ChatGPT7 = Q_7_5;

This shows that the ELSE only applies to one variable ChatGPT41. The other following variable assignments are not conditional in any way. So any values set previously in the do loops would be unconditionally set.

I suspect you may have meant for the Else to look more like this:

IF Q_4_1_1 NE . THEN DO;
   ChatGPT41 = Q_4_1_1; 
   ChatGPT42 = Q_4_1_2; 
   ChatGPT43 = Q_4_1_3; 
   ChatGPT5 = Q_5_1; 
   ChatGPT6 = Q_6_1; 
   ChatGPT7 = Q_7_1;
END;

IF Q_4_2_1 NE . THEN DO;
   ChatGPT41 = Q_4_2_1; 
   ChatGPT42 = Q_4_2_2; 
   ChatGPT43 = Q_4_2_3; 
   ChatGPT5 = Q_5_2; 
   ChatGPT6 = Q_6_2; 
   ChatGPT7 = Q_7_2;
END;
IF Q_4_3_1 NE . THEN DO;
   ChatGPT41 = Q_4_3_1; 
   ChatGPT42 = Q_4_3_2; 
   ChatGPT43 = Q_4_3_3; 
   ChatGPT5 = Q_5_3; 
   ChatGPT6 = Q_6_3; 
   ChatGPT7 = Q_7_3;
END;
IF Q_4_4_1 NE . THEN DO;
   ChatGPT41 = Q_4_4_1; 
   ChatGPT42 = Q_4_4_2; 
   ChatGPT43 = Q_4_4_3; 
   ChatGPT5 = Q_5_4; 
   ChatGPT6 = Q_6_4; 
   ChatGPT7 = Q_7_4;
END;
ELSE do;
   ChatGPT41 = Q_4_5_1; 
   ChatGPT42 = Q_4_5_2; 
   ChatGPT43 = Q_4_5_3; 
   ChatGPT5 = Q_5_5; 
   ChatGPT6 = Q_6_5; 
   ChatGPT7 = Q_7_5;
end;

HOWEVER that else ONLY applies when the IF Q_4_4_1 is missing ( So values that should have been assigned in the other IF then do are replaced. For example if all of Q_4_1_1, Q_4_2_1, Q_4_3_1, Q_4_4_1 are not missing which assignment should take priority? You do have 4 Do loops.

 

You have something like 30+ variables with no provided values or expected results. You should fix that, as in provide values for the variables and the expected results.

 

Personally seeing ChatGPT in those variables makes be cringe that some other terrible almost working code is being modified blindly without a clear knowledge of what the code is supposed to actually do.

 

View solution in original post

6 REPLIES 6
Kurt_Bremser
Super User

ELSE is part of the immediately preceding IF.

To build a chain of mutually exclusive branches, you need to use

if /* condition 1 */
then /* branch 1 */;
else if /* condition 2 */
then /* branch 2 */;
else if /* condition 3 */
then /* branch 3 */;
else /* global else branch */;
Kurt_Bremser
Super User

And you have no DO loop here, only several DO/END blocks. "Loop" is used for repeating DO uses, like

do x = 1 to 5,
end;

(iterative)

or DO WHILE (condition)

or DO UNTIL (condition)

newtriks
Obsidian | Level 7

Thanks for your help!

Patrick
Opal | Level 21

@newtriks Can you please mark the answer that helped you most as the solution so the rest of us know that your question has been resolved. ...and in the future others with a similar problem searching the communities can find the solution quicker.

ballardw
Super User

As a minimum on suspects that your ELSE is improperly structured.

First, restructure the code for a certain amount of legibility:

IF Q_4_1_1 NE . THEN DO;
   ChatGPT41 = Q_4_1_1; 
   ChatGPT42 = Q_4_1_2; 
   ChatGPT43 = Q_4_1_3; 
   ChatGPT5 = Q_5_1; 
   ChatGPT6 = Q_6_1; 
   ChatGPT7 = Q_7_1;
END;

IF Q_4_2_1 NE . THEN DO;
   ChatGPT41 = Q_4_2_1; 
   ChatGPT42 = Q_4_2_2; 
   ChatGPT43 = Q_4_2_3; 
   ChatGPT5 = Q_5_2; 
   ChatGPT6 = Q_6_2; 
   ChatGPT7 = Q_7_2;
END;
IF Q_4_3_1 NE . THEN DO;
   ChatGPT41 = Q_4_3_1; 
   ChatGPT42 = Q_4_3_2; 
   ChatGPT43 = Q_4_3_3; 
   ChatGPT5 = Q_5_3; 
   ChatGPT6 = Q_6_3; 
   ChatGPT7 = Q_7_3;
END;
IF Q_4_4_1 NE . THEN DO;
   ChatGPT41 = Q_4_4_1; 
   ChatGPT42 = Q_4_4_2; 
   ChatGPT43 = Q_4_4_3; 
   ChatGPT5 = Q_5_4; 
   ChatGPT6 = Q_6_4; 
   ChatGPT7 = Q_7_4;
END;
ELSE ChatGPT41 = Q_4_5_1; 
ChatGPT42 = Q_4_5_2; 
ChatGPT43 = Q_4_5_3; 
ChatGPT5 = Q_5_5; 
ChatGPT6 = Q_6_5; 
ChatGPT7 = Q_7_5;

This shows that the ELSE only applies to one variable ChatGPT41. The other following variable assignments are not conditional in any way. So any values set previously in the do loops would be unconditionally set.

I suspect you may have meant for the Else to look more like this:

IF Q_4_1_1 NE . THEN DO;
   ChatGPT41 = Q_4_1_1; 
   ChatGPT42 = Q_4_1_2; 
   ChatGPT43 = Q_4_1_3; 
   ChatGPT5 = Q_5_1; 
   ChatGPT6 = Q_6_1; 
   ChatGPT7 = Q_7_1;
END;

IF Q_4_2_1 NE . THEN DO;
   ChatGPT41 = Q_4_2_1; 
   ChatGPT42 = Q_4_2_2; 
   ChatGPT43 = Q_4_2_3; 
   ChatGPT5 = Q_5_2; 
   ChatGPT6 = Q_6_2; 
   ChatGPT7 = Q_7_2;
END;
IF Q_4_3_1 NE . THEN DO;
   ChatGPT41 = Q_4_3_1; 
   ChatGPT42 = Q_4_3_2; 
   ChatGPT43 = Q_4_3_3; 
   ChatGPT5 = Q_5_3; 
   ChatGPT6 = Q_6_3; 
   ChatGPT7 = Q_7_3;
END;
IF Q_4_4_1 NE . THEN DO;
   ChatGPT41 = Q_4_4_1; 
   ChatGPT42 = Q_4_4_2; 
   ChatGPT43 = Q_4_4_3; 
   ChatGPT5 = Q_5_4; 
   ChatGPT6 = Q_6_4; 
   ChatGPT7 = Q_7_4;
END;
ELSE do;
   ChatGPT41 = Q_4_5_1; 
   ChatGPT42 = Q_4_5_2; 
   ChatGPT43 = Q_4_5_3; 
   ChatGPT5 = Q_5_5; 
   ChatGPT6 = Q_6_5; 
   ChatGPT7 = Q_7_5;
end;

HOWEVER that else ONLY applies when the IF Q_4_4_1 is missing ( So values that should have been assigned in the other IF then do are replaced. For example if all of Q_4_1_1, Q_4_2_1, Q_4_3_1, Q_4_4_1 are not missing which assignment should take priority? You do have 4 Do loops.

 

You have something like 30+ variables with no provided values or expected results. You should fix that, as in provide values for the variables and the expected results.

 

Personally seeing ChatGPT in those variables makes be cringe that some other terrible almost working code is being modified blindly without a clear knowledge of what the code is supposed to actually do.

 

newtriks
Obsidian | Level 7

That did the trick. Thanks so much for your help!

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

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
  • 6 replies
  • 788 views
  • 0 likes
  • 4 in conversation