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
Error unclosed Do block
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.
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;
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;
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
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
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!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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: