BookmarkSubscribeRSS Feed
BrahmanandaRao
Lapis Lazuli | Level 10
data even_odd;
do i= 1 to 100 ;
if mod(i,2)=0 then do;
number='Even' ;
end;

else if mod(i,2)=1 then do ;
number ='Odd';
end;
run;

I am using if then do else if do 

to get even and odd result in number variable column

7 REPLIES 7
BrahmanandaRao
Lapis Lazuli | Level 10

Error unclosed Do block 

Kurt_Bremser
Super User

Against a gazillion of advice to the contrary, you still refuse to use proper code formatting with indentation, which makes finding such an issue a breeze. If you are not interested in advice, why do you come here at all?

 

Start by using consistent indentation for your do/end blocks, and the problem will jump into your eyes so hard that I have to fear for your future eyesight.

 

BrahmanandaRao
Lapis Lazuli | Level 10
data even_odd;
do i= 1 to 100 ;
	if mod(i,2)=0 then do;
number='Even' ;output;
		end;

	else if mod(i,2)=1 then do ;
number ='Odd';output;
	end;
	end;
run;
Kurt_Bremser
Super User

Don't use tabs in code, use a consistent number of blanks. SAS Studio and Enterprise Guide provide options for replacing tabs with blanks while you type code, my "fictional" tab width is always set to 2, so my recommendation for  formatted code looks like this:

data even_odd;
do i = 1 to 100;
  if mod(i,2) = 0
  then do;
    number = 'Even';
    output;
  end;
  else do;
    number ='Odd';
    output;
  end;
end;
run;

The second IF is not necessary as the MOD function with a second parameter of 2 can only return 0 or 1.

 

But you can make your code even simpler:

data even_odd;
length i 8 number $5;
do i = 1 to 100;
  number = ifc(mod(i,2),"Odd","Even");
  output;
end;
run;
yabwon
Onyx | Level 15

One more way of "readable" indentation would be:

data even_odd;
  do i = 1 to 100;
    if mod(i,2) = 0 then 
      do;
        number = 'Even';
        output;
      end;
    else 
      do;
        number ='Odd';
        output;
      end;
  end;
run;

 

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



ballardw
Super User
data even_odd;
   do i= 1 to 100 ;
      if mod(i,2)=0 then do;
         number='Even' ;
      end;

      else if mod(i,2)=1 then do ;
         number ='Odd';
      end;

/* see anything missing*/
run;

Line up your code and things are easier to see exactly where the problem is.

 

"Error unclosed Do block" means that you have a DO without an END

Ready to join fellow brilliant minds for the SAS Hackathon?

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!
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
  • 7 replies
  • 2237 views
  • 4 likes
  • 4 in conversation