You were already told how to get it to work. You need to put your %IF ... %THEN %DO ... %END inside of a macro and then call the macro. This is just 3 lines of code to add so why do you need someone else to write it for you?
Asking why your macro doesn't work is not the issue. You need to show others that you are, at the very least, listening to them and putting forth some effort. sbb told you what you needed to do to get the code working and your reply was, "thanks for the advise. But can you please give out the fix that makes the code running because I'm tired of the piece of code." Asking what he meant by put the code within a macro would have been ok. Asking someone to do it for you because you are "too tired" is insulting. It implies that you think their time is worth less than your's.
Your thinking is naively deep and in a wrong direction. No one's time is more valuable professionally than other's in this forum. We come here to seek solutions or "shortcuts" and enjoy the programming in SAS. Please don't think any other ways, otherwise please just have a laugh and don't reply.
It is not trivial to write a general macro that returns the number of observations in a SAS data set. Read Jack Hamilton's famous SUGI26 paper to see the reasons why this is so. You can see his macro source code in the appendix of the paper.
It is rare that you really need to know how many obs. Usually just knowing SOME or NONE is usually adequate. This is evidenced by your code
[pre]%if &gRetVal > 0 %then %do;[/pre]
I would suggest a more conservative approach that uses a simple data step.
stop; *remove(or comment) to get obs;
msg = 'No observations present in the dataset.';
attrib msg label='~';
if eof then do;
call execute('proc print split="~" noobs data=noobs; run;');
else call execute('proc print data=class; run;');
set class(drop=_all_) end=eof;