BookmarkSubscribeRSS Feed
Peter_C
Rhodochrosite | Level 12
CharlesR

although you are new to SAS you may not be new to such IF/THEN logic.
To watch the data step flow, I would really recommend using the data step option /DEBUG ; [pre]data output_data_set /debug ;
do whatever ;
if whatever ........ ;
end ;
run ;[/pre]
Debugger will let you step through your code and examine variable values - but does not work in SAS Enterprise Guide - yet.

good luck
peterC
Cynthia_sas
SAS Super FREQ
I'm responding to this statement:

Ah. I'm quite new to SAS and was somehow under the impression that my IF/THEN loop had to have the DO statement in order to work.


Here's the deal....Let's start with a simple IF statement and a simple IF/ELSE:
[pre]
if wombat = 'true' then < put 1 and only 1 statement here > ;

OR

if wombat = 'true' then < put 1 and only 1 statement here > ;
else < put a single statement here > ;
[/pre]

The very nanosecond that you say to yourself...oh gosh, I need to have 2 or 3 statements get executed if wombat = 'true' then you are into the world of the DO/END block (or as we used to say in my programming class -- the structured IF/DO/END):
[pre]
if wombat = 'true' then do;
< put 1 statement here > ;
< put statement 2 here > ;
< put statement 3 here > ;
end;

OR

if wombat = 'true' then do;
< put 1 statement here > ;
< put statement 2 here > ;
< put statement 3 here > ;
end;
else do;
< put diff statement 1 here > ;
< put diff statement 2 here > ;
< put diff statement 3 here > ;
end;

OR, even -- transmute the simple IF to an IF/DO/END:

if wombat = 'true' then do;
< put 1 and only 1 statement here > ;
end;
else do;
< put a single statement here > ;
end;
[/pre]

In fact, my SAS mentor was something of a curmudgeon about NOT using the simple form of the IF statement -- on the a theory that if you used the IF/DO/END construction, even when you only needed to execute 1 statement on the true and another single statement on the false -- it would be easier for you maintenance-wise and in the long run (when you had to modify the code to add more statements) to just have the DO/END used all the time. Then it was easy to document:
[pre]
if wombat = 'true' then do; /* when wombat is true, set foobar to 1 */
< put 1 and only 1 statement here > ;
end; /* end wombat true */
else do; /* when wombat is false, set foobar to 0 */
< put a single statement here > ;
end; /* end wombat false */
[/pre]

Now that may seem overkill in the documentation department but consider the nested DO possibilities:
[pre]
if wombat = 'true' then do; /* when wombat is true, set foobar to 1 */
< put 1 and only 1 statement here > ;
if character = 'kermit' and street = 'sesame' then do; /* kermit test */
< put 1 statement here > ;
< put statement 2 here > ;
end; /* end for kermit test */
end; /* end wombat true */
else do; /* when wombat is false, set foobar to 0 and test character and hat*/
< put a statement here > ;
if character = 'tom terrific' and hat = 'pointed' then do; /* tt and hat test */
< put 1 statement here > ;
< put statement 2 here > ;
end; /* end tom terrific and hat test */
else do; /* do not care about any other character */
< put a statement here > ;
end; /* end for tom terrific when wombat is false */
end; /* end wombat false */
[/pre]

and I know that there have wars fought over how the IF, THEN, DO and END should be indented and on which line the DO should appear...but I like to see a semi-colon at the end of each line and I like to have the ENDs line up with the IFs and the DO at the end of the THEN. But that's just me.

Without getting into the world of LOOPS, that's a somewhat artificial explanation of how IF statements work in SAS. You don't NEED to have a DO/END if you are only going to execute 1 statement, but you DO need to have a DO/END if you need to have 2 or more statements executed.

I now always follow the DO/END recommendation of my mentor, even if for 1 statement scenario....more often than not, I do go back and add more statements and thinking about the conditions and the DO/END, makes me clarify my logic. And for really, really complicated logic, I haul out my flowchart ruler and draw it out. Sometimes, the picture makes the coding easier.

my .02,

cynthia
CharlesR
Calcite | Level 5
Yep, you're right. I was trying to get all "fancy" by having the nested loops/do end statements, and i needed to break it down to my 8 permutations with 8 if/else if's. More straight forward this way.

Thanks!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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