DATA Step, Macro, Functions and more

If then else block

Reply
Occasional Contributor
Posts: 13

If then else block

Hi Community,

 

I am facing an systax error and expecting something after running this. Please guide me what is the correct one

 

compute Index_Id;

    if Index_Id = '0' then

call define(_row_,"style","style={just=1 font face=Arial font_style=italic font_size=2}");

else

if((Caol1=(' ' | '0.00%') and col2=(' ' | '0.00%'))

then do;

col1=' ';

col2=' ';

end;

endcomp;

 

Please help me out.

Trusted Advisor
Posts: 1,349

Re: If then else block

[ Edited ]

You posted only a part of your program code.

 

You should post, also, the log with the syntx error you got.

 

Or maybe your error is:

    if((Caol1=(' ' | '0.00%') and col2=(' ' | '0.00%'))

should it be col1 instead Caol1 ???

Occasional Contributor
Posts: 13

Re: If then else block

Sorry for creating the confusion.. i am changing no its col1.. i have done the typing mistake here..i have attachd the log here..


20161214_140328.jpg
Super User
Posts: 6,926

Re: If then else block

The ERROR points to the "then" keyword, indicating a problem with the condition. Counting your parentheses I see 11 opening, but only 10 closing parentheses.

 

Once again you stumble into the "spaghetti code hole". As soon as logical code segments go beyond one line, proper use of indentation and linebreaks to separate the elements is a MUST!

 

Also make use of a feature of the enhanced editor: when putting the cursor to a closing parenthesis, the editor will show you the opening one that it thinks is paired with it.

 

Seeing that you only have and's in your condition, most of your parentheses are unnecessary, anyway. Parentheses to separate condition elements are only necessary when "or" and "and" are mixed and the rule "and goes before or" has to be circumvented.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 6,926

Re: If then else block

[ Edited ]

And, BTW: just copy/paste the log into a {i} subwindow here; downloading the image takes time, and one can't copy/paste code from an image for testing/editing. Don't expect us to type code for you when it's absolutely not necessary.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 6,926

Re: If then else block

Posting the log with the syntax error in a {i} window would greatly help Smiley Sad

 

From your earlier posts, I think that caol1 is not present in the dataset. And the only variables that can be used in the compute statement are group or order variables for a compute target.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 13

Re: If then else block


20161214_140328.jpg
Super User
Super User
Posts: 7,392

Re: If then else block

What the chaps above are asking is that you don't post pictures (or other documents) but use simple copy and paste functionality to copy the text from your program into the post you are writing.  It is annoying and difficult to use pictures for anything.  

You can also use the code window function in your posts.  Above where you type replies there is a tool bar containing {i}, and other tools icons.  Use this to post code and logs as it stands out and keeps formatting.

 

Now onto your problem.  As mentioned above, you have unbalanced quote, bracket or other containing special characters.  We can't tell really from that picture where it is.  So step through your code, check each line works.  Count that the number of ) = number of these (.

 

This is could debugging and is something you will do all the time in programming.  Note you can comment out section of code by highlighting and pressing cntrl + shift + / and uncomment with cntrl + shift + \.  makes debugging easier.

Super User
Posts: 5,069

Re: If then else block

Using the same style of syntax that you have been, your code is missing a closing parenthesis:

 

else if ((One_Day_Un_Return2=(' ' | '0.00%'))

 

However, once that is cleared up, there are many more issues with the code.  I would guess that your intention is to check each variable to see whether it is either blank or contains the text "0.00%".  If that's the case, the proper syntax would be:

 

else if One_Day_Un_Return2 in (' ', ' 0.00%') ......;

 

If your intention is something else you will have to add some details about it.  But even the fixed code (adding the parenthesis) will not run.

 

 

Occasional Contributor
Posts: 13

Re: If then else block

Hi,

 

Thank you for your support.

Actually the requirement is to check the value of a column if '0.00%' then it would be blank.

First of all to check for a condition that if the value of a column is 0 then make it as '0.00%'. I have done it to make it as char fist using put() then || with percentage.Its working fine.

For the next case While I am trying to check if the column value is '0.00%' then make it as blank,dats not working. I feel that when I am trying to check if the value is equal to '0.00%' then make it as col1=' ' ,it simply make it as blank || %. which cant match the condition anymore. thats why expected output is not coming.

can you please help me how to resolve this one??

 

thanks,

Puja

Super User
Super User
Posts: 7,392

Re: If then else block

Hi,

 

If you post some test data in the form of a datastep we could provide working code.  I am unsure as to why you need to convert a value before you check if it is 0 or not?

data want;
  length want_text $20;
  input myval;
  want_text=ifc(myval=0,"",put(myval,percent7.1));
datalines;
0
0.345
;
run;
Ask a Question
Discussion stats
  • 10 replies
  • 282 views
  • 2 likes
  • 5 in conversation