DATA Step, Macro, Functions and more

I need explanation for this code and the macro

Accepted Solution Solved
Reply
Regular Contributor
Posts: 161
Accepted Solution

I need explanation for this code and the macro

     options yearcutoff=1950;
     
     %macro y2kopt(date2);
        %if &date2 >= 14610 %then %do;
           options yearcutoff=2000;
        %end;
        %else %do;
           options yearcutoff=1900;
        %end;
     %mend;

     %let date=14610;
     options yearcutoff=1920;

     %y2kopt(&date)

The SAS date for January 1, 2000, is 14610. What is the value of YEARCUTOFF when the macro Y2kopt finishes execution?

 

 a.  1900
 b.  1920
 c.  1950
 d.  2000

Accepted Solutions
Solution
2 weeks ago
SAS Super FREQ
Posts: 9,371

Re: I need explanation for this code and the macro

Hi:

  The macro program is very straightforward and if you have studied the Macro Language, then it should be very easy to read the macro program.

 

  In pseudo-code:

If the value of macro variable &date2 is GE 14610 then issue an options statement for YEARCUTOFF=2000

else If the value of macro variable &date2 is LT 14610 then issue an options statement for YEARCUTOFF=1900

 

  The %MACRO/%MEND is causing the macro program Y2KOPT to be compiled and stored until it is ready to be used. The macro is defined as having 1 positional parameter named DATE2 and referred to in the macro program as &DATE2.

 

  The program starts with an OPTIONS statement that initially sets YEARCUTOFF to 1950, but that is really a distractor, as is the other OPTIONS statement that sets YEARCUTOFF to 1920.

 

  The %LET statement that is outside the macro definition program creates a Global Macro variable called &DATE with a value of 14610 (and the question tells you that this value 14610, when it represents a date is the SAS date for January 1, 2000, which is nice to know, but the macro program uses the numeric value).

 

  The macro program is invoked with the statement:

 %y2kopt(&date)

and that means it is essentially the same as having:

%y2kopt(14610)

 

Which means that the parameter &DATE2 will hold the value 14610. So, the %IF test will be true and the first OPTIONS statement in the macro program will be sent for execution.

 

You can prove this to yourself by using some debugging techniques like MPRINT/SYMBOLGEN and using %PUT along with testing the value of YEARCUTOFF at every step along the way, as shown below:

yearcutoff.png

 

  Hope this helps,

Cynthia

 

View solution in original post


All Replies
Super User
Posts: 10,278

Re: I need explanation for this code and the macro


@mona4u wrote:
     options yearcutoff=1950;
     
     %macro y2kopt(date2);
        %if &date2 >= 14610 %then %do;
           options yearcutoff=2000;
        %end;
        %else %do;
           options yearcutoff=1900;
        %end;
     %mend;

     %let date=14610;
     options yearcutoff=1920;

     %y2kopt(&date)

The SAS date for January 1, 2000, is 14610. What is the value of YEARCUTOFF when the macro Y2kopt finishes execution?

 

 a.   1900
 b.   1920
 c.   1950
 d.   2000

Try it.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Solution
2 weeks ago
SAS Super FREQ
Posts: 9,371

Re: I need explanation for this code and the macro

Hi:

  The macro program is very straightforward and if you have studied the Macro Language, then it should be very easy to read the macro program.

 

  In pseudo-code:

If the value of macro variable &date2 is GE 14610 then issue an options statement for YEARCUTOFF=2000

else If the value of macro variable &date2 is LT 14610 then issue an options statement for YEARCUTOFF=1900

 

  The %MACRO/%MEND is causing the macro program Y2KOPT to be compiled and stored until it is ready to be used. The macro is defined as having 1 positional parameter named DATE2 and referred to in the macro program as &DATE2.

 

  The program starts with an OPTIONS statement that initially sets YEARCUTOFF to 1950, but that is really a distractor, as is the other OPTIONS statement that sets YEARCUTOFF to 1920.

 

  The %LET statement that is outside the macro definition program creates a Global Macro variable called &DATE with a value of 14610 (and the question tells you that this value 14610, when it represents a date is the SAS date for January 1, 2000, which is nice to know, but the macro program uses the numeric value).

 

  The macro program is invoked with the statement:

 %y2kopt(&date)

and that means it is essentially the same as having:

%y2kopt(14610)

 

Which means that the parameter &DATE2 will hold the value 14610. So, the %IF test will be true and the first OPTIONS statement in the macro program will be sent for execution.

 

You can prove this to yourself by using some debugging techniques like MPRINT/SYMBOLGEN and using %PUT along with testing the value of YEARCUTOFF at every step along the way, as shown below:

yearcutoff.png

 

  Hope this helps,

Cynthia

 

Frequent Contributor
Posts: 140

Re: I need explanation for this code and the macro

I would recommend reading this book

 

https://www.sas.com/store/books/categories/usage-and-reference/carpenter-s-complete-guide-to-the-sas...

 

And then I bet your understanding would be spot on!

☑ This topic is solved.

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

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