BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
itemsong
Calcite | Level 5

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

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

4 REPLIES 4
Reeza
Super User

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;

itemsong
Calcite | Level 5

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?

Tom
Super User Tom
Super User

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.

Tom
Super User Tom
Super User

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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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