DATA Step, Macro, Functions and more

If clause

Reply
Super Contributor
Posts: 436

If clause

When I run the following code, I got 'not success' in the log where as I'm execpting 'sucess' message in the log. Can someone explain this code?

 

%let end_title=1565;
%macro test;
data test1;
if %eval(&end_title)<1565< %eval(&end_title)+%eval(&end_title) then put 'success';
else put "notsucess";
_n=%eval(&end_title)+%eval(&end_title);
run;
%mend test;
%test
Super User
Posts: 5,511

Re: If clause

That's a question that pops up from time to time .,.. here's oen thread you can look at:

 

https://communities.sas.com/t5/Base-SAS-Programming/Macro-resolving/m-p/218619#M40282

 

 

Super Contributor
Posts: 436

Re: If clause

Posted in reply to Astounding

Could you please tell me how to print the value of _n_  in log from the following code? I could not see the value of _n_ after placing the put _all_ statement.

 

 

if 1<_n_< %eval(&end_title) then do; put option;end;
put _all_;
if _n_=%eval(&end_title) then do; put option;		
put _all_;

 

Super User
Super User
Posts: 7,970

Re: If clause

Hi,

 

Lets start again.  What are you attempting to do.  There is nothing there (again) which needs macro code.  There is then the difference in the code you posted, in the first instance you use _n which is a variable or macro variable?  in the latest post you use _n_ wihch is an automatic variable for row number.  What is the purpose, the logic, i.e. what goes in (as a datastep) and what do you want the output to look like.

Super Contributor
Posts: 436

Re: If clause

My first and second post is not related to each other. I missed to start a new thread for my second post. I need to get the value the _n_ in SAS log. When I tried this code, I'm not getting the value of _n_ in SAS log.

 

if 1<_n_< %eval(&end_title) then do; put option;end;
put _all_;
if _n_=%eval(&end_title) then do; put option;		
put _all_;

I tried this code as well, but could not suceeed. 

if 1<_n_< %eval(&end_title) then do; put option;end;
put _n_=;
if _n_=%eval(&end_title) then do; put option;		
put _n_=;
Super User
Posts: 5,511

Re: If clause

put _all_ will include writing _n_.  If you want _n_ only, you could try:

 

put _n_=;

Super Contributor
Posts: 436

Re: If clause

Posted in reply to Astounding
I did tried as I said my previous, but I could not see the value of _n_ in SAS log.
Super User
Super User
Posts: 7,970

Re: If clause

Sorry, you still have not explained what you are trying to do, what is the input data, what is the output requirement.  The reason is in base SAS, this works fine:

data tmp;
set sashelp.cars;
put _n_;
run;

 

It prints the row number for each record in the dataset.  I can't see what your trying to do.

Super Contributor
Posts: 436

Re: If clause

Posted in reply to Astounding

I'm not getting the value of _n_ after I place the end statement. I should have get the value of _n_ for the first do lopp atleast, but not.

Super User
Super User
Posts: 7,054

Re: If clause

You seem to be missing a END for your second DO.

Super User
Super User
Posts: 7,054

Re: If clause

Looks like it is working to me.

1565 is NOT less than 1565.  So the ELSE clause should execute.

Frequent Contributor
Posts: 144

Re: If clause

After macro replacement your if clause is

if 1565<1565<1565+1565 then put 'success';
else put "notsucess";

1565 is not les than 1565 so the else clause is executed and print "notsucess" message
Respected Advisor
Posts: 4,925

Re: If clause

Logic:

1565 < 1565 < 1565+1565 is equivalent to 

1565 < 1565 < 3130 is equivalent to

1565 < 1565 and 1565 < 3130

since 1565 is not < 1565, the result is FALSE

hence "not success".

PG
Ask a Question
Discussion stats
  • 12 replies
  • 308 views
  • 4 likes
  • 6 in conversation