DATA Step, Macro, Functions and more

DO statements missing semicolon

Accepted Solution Solved
Reply
New Contributor
Posts: 4
Accepted Solution

DO statements missing semicolon

Do statement needs a semicolon as below. 

 

DO; 

SAS satement ; 

END;

 

 

But recently, I happend to omit the semicolon next to DO statement and it still works and generated the same results as with semicolon. Does that mean that I don't have to worry about the semicolon for DO statement?

 

Thank you in advance for your help. 


Accepted Solutions
Solution
‎11-30-2016 09:46 AM
Super User
Posts: 5,516

Re: DO statements missing semicolon

Well, I had a chance to think about this on the ride home.  If that's the question, I think you're right.  The closest I could imagine was this:

 

data testing;

before = time();

do i=1 to 10000000;

   do after = time();

   end;

end;

diff = after - before;

run;

 

Compare that with the added semicolon:

 

data testing2;

before = time();

do i=1 to 10000000;

   do;

   after = time();

   end;

end;

diff = after - before;

run;

 

Now if for some reason the programs differ in their execution time, DIFF would change.  Even so, that's a lot of imagination with zero practicality.

View solution in original post


All Replies
Super User
Posts: 19,869

Re: DO statements missing semicolon

 

It depends. What kind of code did you have after DO?

 

You can include certain statements after a DO that control the loop. 

And others will generate an error. 

 

In fact, I'd say it's unusual to see just a DO, it would be something like

 

if x=1 then do;

 

or 

 

Do i=1, 2, 3 6;

 

http://support.sas.com/documentation/cdl/en/lestmtsref/69738/HTML/default/viewer.htm#p1cydk5fq0u4bfn...

 

 

 

New Contributor
Posts: 4

Re: DO statements missing semicolon

Thank you for your opinion. 

 

Yes, it was in IF/THEN statement. I was expecting an error message but there was not. That's why I was confused. 

 

Let me try it with some other data and see how it works. 

 

Thank you again, 

MJ

Trusted Advisor
Posts: 1,022

Re: DO statements missing semicolon

I guess what your are really asking is:  SAS appears to not object in certain cases when you forget to terminate the DO statement with a semi-colon.  And in those cases, if SAS doesn't object, can you be sure the result will be the same as it would be with the semi-colon in place?

 

I don't know, but I wouldn't trust it.

 

At the moment, I can't think of a counterexample. 

 

 

For instance these two support the "no difference" conjecture, but that's not proof:

  do;

   x=1;

   y=3*x;

  end;

 

and

 

  do

   x=1;

   y=3*x;

  end;

 

regards,

Mark

 

 

New Contributor
Posts: 4

Re: DO statements missing semicolon

Thank you for your attention on this topic. 

 

Yes, I was confused cuz it didn't give me any error message and generated the same result.

 

I don't know why this still works.. need to search more. 

 

Thank you again, 

MJ

Super User
Posts: 5,516

Re: DO statements missing semicolon

The key to removing the semicolon is that the result has to create a valid DO statement.  So if the next statement is:

 

name='Fred';

 

That works, because this would be a valid DO loop:

 

do name='Fred';

 

It both sets NAME to "Fred" and performs any additional statements within the loop.

 

But if the next statement wouldn't form a valid DO statement, you will get an error.  A few examples where omitting the semicolon gives you an error because the resulting DO statement is no longer correct syntax:

 

if name='Fred' then salary='High';

 

substr(name, 4,1)='t';

 

select (name);

 

array names {1} name;

 

retain total 0;

Trusted Advisor
Posts: 1,022

Re: DO statements missing semicolon

Posted in reply to Astounding

Yes, but in your example ("do  if ...."), SAS would send an error message, thereby protecting the programmer from the oversight. 

 

I think the OP's issue is: if there's no such error message, can there ever be a difference in results attributable to the missing semi-colon?

 

M K

Solution
‎11-30-2016 09:46 AM
Super User
Posts: 5,516

Re: DO statements missing semicolon

Well, I had a chance to think about this on the ride home.  If that's the question, I think you're right.  The closest I could imagine was this:

 

data testing;

before = time();

do i=1 to 10000000;

   do after = time();

   end;

end;

diff = after - before;

run;

 

Compare that with the added semicolon:

 

data testing2;

before = time();

do i=1 to 10000000;

   do;

   after = time();

   end;

end;

diff = after - before;

run;

 

Now if for some reason the programs differ in their execution time, DIFF would change.  Even so, that's a lot of imagination with zero practicality.

New Contributor
Posts: 4

Re: DO statements missing semicolon

Thank you for your opinion. 

 

Yes I expected an error message but it runs without error message. 

I thought that SAS code is very sensitive on Semicolon and operators. 

I need to search little more detail about this. 

 

Thank you again, 

MJ

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 318 views
  • 2 likes
  • 4 in conversation