DATA Step, Macro, Functions and more

Evaluating macros with non-macros

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 5
Accepted Solution

Evaluating macros with non-macros

I basically want to check if my inputted year (through the macro), is the same as the year for the variable x (a SAS formatted date).

I cam up with two options:

Is this a valid piece of code?

%macro test(year);

x=year(date);

%if &year.=x %then %do;

*blah blah blah;

%end;

What about this next one?

%let j = %eval(x - &year.);

if &j.=0 then do;

*blah blah blah;

end;

Thanks


Accepted Solutions
Solution
‎05-22-2013 09:18 PM
Super User
Super User
Posts: 6,499

Re: Evaluating macros with non-macros

Macro logic (%IF/%THEN/%ELSE...) are for when you want the macro to conditional output SAS CODE that SAS can then execute.

Once the code is generated and the generated DATA step or PROC is running the macro programming is mainly irrelevant.

So if year(date)=1995, and &year=1995, then the "do" will be executed?

If you want to conditionally execute statements based on whether the DATE variable's value for the current observation being processed is equal to the user specified year then you would code

   IF YEAR(DATE) = &YEAR THEN DO;

     .....

  END;

If the user calls the macro with YEAR set to 1995 ( %test(year=1995) ) then the code above will become :

   IF YEAR(DATE) = 1995 THEN DO;

     .....

  END;

Turn on the MPRINT option so that you can see what code the macro has generated.

View solution in original post


All Replies
Super User
Posts: 17,784

Re: Evaluating macros with non-macros

I think your mixing up macro code and data step code.

First part the answer is no.

What is X? It isn't in a data set or stored anywhere. You can make it a macro variable. But to evaluate functions not in a datastep you need to use either %eval or %sysfunc depending on what you're trying to do.

The code that is BLAH BLAH must also be valid.


If the second set of code isn't in a datastep then it also isn't valid.


The first one is closest I think, except where does the date come from? Is it a variable because it doesn't look to be in a data step either. 


You probably need to provide more details of what you're trying to do.


*if date is a macro variable and this is in macro code;

%if &year.=%sysfunc(year(&date)) %then %do;


or


*if date is a variable and this is in datastep code;

if year(date)=&year then do;

Occasional Contributor
Posts: 5

Re: Evaluating macros with non-macros

So this is all within a datastep--sorry I should've provided the entire code..

So for this:

if year(date)=&year then do;


Do I not need the %if and %then and %do?


So if year(date)=1995, and &year=1995, then the "do" will be executed?

Solution
‎05-22-2013 09:18 PM
Super User
Super User
Posts: 6,499

Re: Evaluating macros with non-macros

Macro logic (%IF/%THEN/%ELSE...) are for when you want the macro to conditional output SAS CODE that SAS can then execute.

Once the code is generated and the generated DATA step or PROC is running the macro programming is mainly irrelevant.

So if year(date)=1995, and &year=1995, then the "do" will be executed?

If you want to conditionally execute statements based on whether the DATE variable's value for the current observation being processed is equal to the user specified year then you would code

   IF YEAR(DATE) = &YEAR THEN DO;

     .....

  END;

If the user calls the macro with YEAR set to 1995 ( %test(year=1995) ) then the code above will become :

   IF YEAR(DATE) = 1995 THEN DO;

     .....

  END;

Turn on the MPRINT option so that you can see what code the macro has generated.

Super User
Super User
Posts: 6,499

Re: Evaluating macros with non-macros

To test a variable you need to use actual SAS code.  You can reference the macro variable in the code to perform the test.

%macro test(year);

data _null_;

  set mydata ;

  if year(x) = &year then do;

    ....

end;

run;

%mend test;

%test(2010);

Now if you want the result of the test to effect the macro logic then you can use CALL SYMPUTX to create a macro variable in that data step that can be used later in the macro to control the generation of other code.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 185 views
  • 0 likes
  • 3 in conversation