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

Hi, facing problem about the if statement, I have 4 different situation as the code below and the result will depend on the year and month I key in, YYYY is the year while MM is the month. However this code is not work. may I know whats the problem with my code? or my code is totally wrong for this?

 

 

 

 

%macro date(YYYY=2018, MM=11,Y=2018,M=11);%global YR MTH;

 

 

 

 

(BODY CODE.....)

 

 

if &MM=12 then

libname py&Y&M. "\\kaiwksgh415thw5\Data\POLA\Policy\&Y\&Y&M\DBF";

data py&Y&M..Prem_fy&YR._&Y&M._addfinpost;

set py&Y&M..Prem_Reg_POLA_&Y&M.;

 

 

else if &MM=01 then

libname py&Y&M. "\\kaiwksgh415thw5\Data\POLA\Policy\&Y\&Y&M\DBF";

data py&Y&M..prem_fy&Y._&Y&M._addfinpost;

 

set

py&YR&MTH..prem_fy&Y._&YR&MTH._addfinpost

py&Y&M..Prem_Reg_POLA_&Y&M.;

else if &MM=11 then

libname py&Y&M. "\\kaiwksgh415thw5\Data\POLA\Policy\&Y\&Y&M\DBF";

data py&Y&M..prem_fy&Y._&Y&M._addfinpost;

 

set

py&YR&MTH..prem_fy&Y._&YR&MTH._addfinpost

py&Y&M..Prem_Reg_POLA_&Y&M.;

 

else

libname py&Y&M. "\\kaiwksgh415thw5\Data\POLA\Policy\&Y\&Y&M\DBF";

data py&Y&M..prem_fy&Y._&Y&M._addfinpost;

 

set

py&YR&MTH..prem_fy&Y._&YR&MTH._addfinpost

py&Y&M..Prem_Reg_POLA_&Y&M.;

 

 

%mend date;

%date(YYYY=2018,MM=11,Y=2018,M=11);

run;

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

Where to start...?

- You need macro level %if...%then...%else

- no need for date specific librefs. The date specific path will do.

- your sample doesn't explain why you need to pass-in month and year twice. Can &YYYY and &Y be different for a single call of the macro? If not then get rid of the duplicated parameters.

- Your code also doesn't show how you populate YR and MTH so assuming these macro vars will also get values suitable for your code.

- I can't really see 4 different code bits for the 4 different cases you define. May be I'm missing something or you have a chance here to simplify your logic. I didn't touch your four cases in case you need to amend the code within to make it case specific.

%macro date(YYYY=2018, MM=11,Y=2018,M=11);
  %global YR MTH;

  /*  (BODY CODE.....) */
  libname py "\\kaiwksgh415thw5\Data\POLA\Policy\&Y\&Y&M\DBF";

  %if &MM=12 %then
    %do;
      data py.Prem_fy&YR._&Y&M._addfinpost;
        set py.Prem_Reg_POLA_&Y&M.;
      run;
    %end;
  %else %if &MM=01 %then
    %do;

      data py.prem_fy&Y._&Y&M._addfinpost;
        set
          py&YR&MTH..prem_fy&Y._&YR&MTH._addfinpost
          py.Prem_Reg_POLA_&Y&M.;
      run;
    %end;
  %else %if &MM=11 %then
    %do;
      data py.prem_fy&Y._&Y&M._addfinpost;
        set
          py&YR&MTH..prem_fy&Y._&YR&MTH._addfinpost
          py.Prem_Reg_POLA_&Y&M.;
      run;
    %end;
  %else
    %do;
      data py.prem_fy&Y._&Y&M._addfinpost;
        set
          py&YR&MTH..prem_fy&Y._&YR&MTH._addfinpost
          py.Prem_Reg_POLA_&Y&M.;
      run;
    %end;
%mend date;

%date(YYYY=2018,MM=11,Y=2018,M=11);

 

 

View solution in original post

4 REPLIES 4
Kurt_Bremser
Super User

libname is a global statement, to be used outside any procedure or data steps.

Since it is always built the same way from the same macro parameters, there is no need to execute it conditionally. Just do

libname py&Y&M. "\\kaiwksgh415thw5\Data\POLA\Policy\&Y\&Y&M\DBF";

at the start of your macro.

What you try to achieve are different data/set statements, for this you need macro code like this:

libname py&Y&M. "\\kaiwksgh415thw5\Data\POLA\Policy\&Y\&Y&M\DBF";

data py&Y&M..Prem_fy&YR._&Y&M._addfinpost;
set
%if &MM ne12 %then %do;
  py&YR&MTH..prem_fy&Y._&YR&MTH._addfinpost
%end;
  py&Y&M..Prem_Reg_POLA_&Y&M.
;

Note that I simplified your code, as the parts for all months except 12 are apparently identical.

 

BIG HINT:

If code "does not work", ALWAYS (and I mean it!!) post the WHOLE (step or macro) code and the WHOLE log from it. If code is too big for posting here, that's a clear sign you are working the wrong way. Start with small units, get them to run properly, and expand from there. 

Use proper code formatting (see my example), post code with the "little running man" (see my example again) and logs or textual data with the {i} button. PLEASE.

 

Edit: further simplified the code

Patrick
Opal | Level 21

Where to start...?

- You need macro level %if...%then...%else

- no need for date specific librefs. The date specific path will do.

- your sample doesn't explain why you need to pass-in month and year twice. Can &YYYY and &Y be different for a single call of the macro? If not then get rid of the duplicated parameters.

- Your code also doesn't show how you populate YR and MTH so assuming these macro vars will also get values suitable for your code.

- I can't really see 4 different code bits for the 4 different cases you define. May be I'm missing something or you have a chance here to simplify your logic. I didn't touch your four cases in case you need to amend the code within to make it case specific.

%macro date(YYYY=2018, MM=11,Y=2018,M=11);
  %global YR MTH;

  /*  (BODY CODE.....) */
  libname py "\\kaiwksgh415thw5\Data\POLA\Policy\&Y\&Y&M\DBF";

  %if &MM=12 %then
    %do;
      data py.Prem_fy&YR._&Y&M._addfinpost;
        set py.Prem_Reg_POLA_&Y&M.;
      run;
    %end;
  %else %if &MM=01 %then
    %do;

      data py.prem_fy&Y._&Y&M._addfinpost;
        set
          py&YR&MTH..prem_fy&Y._&YR&MTH._addfinpost
          py.Prem_Reg_POLA_&Y&M.;
      run;
    %end;
  %else %if &MM=11 %then
    %do;
      data py.prem_fy&Y._&Y&M._addfinpost;
        set
          py&YR&MTH..prem_fy&Y._&YR&MTH._addfinpost
          py.Prem_Reg_POLA_&Y&M.;
      run;
    %end;
  %else
    %do;
      data py.prem_fy&Y._&Y&M._addfinpost;
        set
          py&YR&MTH..prem_fy&Y._&YR&MTH._addfinpost
          py.Prem_Reg_POLA_&Y&M.;
      run;
    %end;
%mend date;

%date(YYYY=2018,MM=11,Y=2018,M=11);

 

 

Kayla_Tan222
Calcite | Level 5

Hi Patrick, first of all thanks for your reply. my code is then now like this below, but then the system keep saying:

There is no matching %DO statement for the %END. This statement will be ignored,.

What does it mean? is this mean that I cannot use the %if and %Do statement like this?

 

 

 

libname py&Y&M. "\\kaiwksgh415thw5\Data\POLA\Policy\&Y\&Y&M\DBF";

%if &MM=12 %then %DO;

data py&Y&M..Prem_fy&YR._&Y&M._addfinpost;

set py&Y&M..Prem_Reg_POLA_&Y&M.;

%end;

 

 

%else %if &MM=01 %then %do;

data py&Y&M..Prem_fy&Y._&Y&M._addfinpost;

 

set

py&Year&Mth..Prem_fy&YR._&Year&Mth._addfinpost

py&Y&M..Prem_Reg_POLA_&Y&M.;

%end;

 

 

%else %if &MM=11 %then %do;

data py&Y&M..prem_fy&Y._&Y&M._addfinpost;

 

set

py&Year&Mth..Prem_fy&YR._&Year&Mth._addfinpost

py&Y&M..Prem_Reg_POLA_&Y&M.;

%end;

 

 

%else

data py&Y&M..prem_fy&Y._&Y&M._addfinpost;

 

set

py&Year&Mth..Prem_fy&YR._&Year&Mth._addfinpost

py&Y&M..Prem_Reg_POLA_&Y&M.;

%end;

 

Patrick
Opal | Level 21

There is a %DO; missing in your last %else case.

%else
  %do;
  .......

  %end;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 748 views
  • 0 likes
  • 3 in conversation