DATA Step, Macro, Functions and more

Proc SQL - IF/THEN Conditions Process only when condition met

Accepted Solution Solved
Reply
Contributor
Posts: 58
Accepted Solution

Proc SQL - IF/THEN Conditions Process only when condition met

I have a daily process with a some user defined variables that are set, each day by the user before processing. 

 

2 Questions:

1.  I have a query / Proc SQL statement within the processes that I want to run only on Settlement day

 

How could I do this?

 

%Let Settle_DAY_Y_N = "YES"

If  &Settle_DAY_Y_N. = "YES" Then    /*  This is the condition */

Proc SQL;

/*Run SQL here */

/*  Settlement update query here */

run;

 

else

/* Don't Run SQL here */

end;

 

2.  (2nd Question) Would your solution also works for this?

 

%Let Settle_DAY_Y_N = "YES"

 

If  &Settle_DAY_Y_N. = "YES" Then   /*  This is the condition */

 

Data work.Combine_Records;

set work.A work.AA work.AAA;

run;

 

else

Data work.Combine_Records;

set work.A work.AA;   /* Since no settlement record was created drop work.AAA */

run;

end;

 

Many Thanks

 

 


Accepted Solutions
Solution
‎01-31-2017 11:05 AM
Trusted Advisor
Posts: 1,395

Re: Proc SQL - IF/THEN Conditions Process only when condition met

You can do it by using macro program. Pay attention to syntax:

 

%macro doit(Settle_DAY_Y_N); 

        %If  &Settle_DAY_Y_N. = YES %Then %do;      /*  This is the condition */

              Proc SQL;

               /*Run SQL here */

              /*  Settlement update query here */

             run;

       %end;

%mend doit;

%doit;

 

You can enter any sas code between %do;  and  %end;

 

  

View solution in original post


All Replies
Solution
‎01-31-2017 11:05 AM
Trusted Advisor
Posts: 1,395

Re: Proc SQL - IF/THEN Conditions Process only when condition met

You can do it by using macro program. Pay attention to syntax:

 

%macro doit(Settle_DAY_Y_N); 

        %If  &Settle_DAY_Y_N. = YES %Then %do;      /*  This is the condition */

              Proc SQL;

               /*Run SQL here */

              /*  Settlement update query here */

             run;

       %end;

%mend doit;

%doit;

 

You can enter any sas code between %do;  and  %end;

 

  

Contributor
Posts: 58

Re: Proc SQL - IF/THEN Conditions Process only when condition met

Shmuel,

 

Thank you very much1

 

Perfect!

 

Kody_Devl

Contributor
Posts: 58

Re: Proc SQL - IF/THEN Conditions Process only when condition met

Shmuel,

 

My version is not evaluating correcly as "YES" (Skips my code) and is probably syntax.  Do you see it?

 

 

%Let Settle_Today_Y_N = 'YES'; /* CAPS YES or NO */

 

%macro Settled(Settle_Today_Y_N);

%If &Settle_Today_Y_N. = 'YES' %Then %do;

proc import datafile = "/sas/sasperm3_prod/yyyyyyy/xxxx/import/&Settled."

out= A_SRC_Settled dbms= xlsx replace;

getnames= yes;

range= 'Settled';

run;

%end;

%mend Settled;

%Settled;

 

Trusted Advisor
Posts: 1,395

Re: Proc SQL - IF/THEN Conditions Process only when condition met

You should define the macro variable either by %LET - then define %MACRO DOIT().

or define the macro variable as an argumnet to the macro program, the way I wrote it:

      %macro doit(Settle_Today_Y_N);   /* just define name to argument)

          ... 

      %mend doit;

      %doit('YES');     /* asign value to argument */

 

But, when you use macro variable you don't need quotes:

       either define %let Settle_Today_Y_N = YES;

       or  define     %doit(YES);

       then check by:   %if &Settle_Today_Y_N = YES %then ...   /* YES without quotes */

 

 

Super User
Posts: 10,521

Re: Proc SQL - IF/THEN Conditions Process only when condition met

And

%else %do;

  <other code>

%end;

 

Macro %if or %do require the code be inside a macro definition %macro/%mend construct to define and the call the macro. Otherwise use of %if or %do will generate error messages of  attempting to use in open code.

☑ This topic is SOLVED.

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

Discussion stats
  • 5 replies
  • 120 views
  • 0 likes
  • 3 in conversation