How to condtionally execute Proc Datasets procedure?

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 137
Accepted Solution

How to condtionally execute Proc Datasets procedure?

Hi, I need help in conditionally executing Proc dataset based on the following logic-

Data process;

k=25;

*create two macro variables using call symput;

run;

proc datasets lib=work;

if macrovar1 ne macrovar2 or missing(K) then delete process;/* this is what i want to achieve, any help please?*/

run;


Accepted Solutions
Solution
‎06-29-2015 06:21 AM
Respected Advisor
Posts: 4,138

Re: How to condtionally execute Proc Datasets procedure?

Proc Datasets doesn't abort/kill a process. It also depends what you mean by "process". Is this a separate SAS program or are you only talking about skipping certain steps within a single program (a single process).

To skip steps in a program the easiest way is to wrap everything into a macro and then use a %GOTO statement SAS(R) 9.4 Macro Language: Reference, Third Edition

macro wrapper();

  Data process;

/*    k=24;*/

    k=25;

    call symputx('k',k);

  run;

  %if &k=25 %then %goto exit;

  data _null_;

    put "Only executes if condition false";

    stop;

  run;

  %exit:

  data _null_;

    put "executes always";

    stop;

  run;

%mend;

%wrapper()

View solution in original post


All Replies
Frequent Contributor
Posts: 144

Re: How to condtionally execute Proc Datasets procedure?

When I need to two that i use a macro,

%macro execute;

     %if &macrovar1 ne &macrovar2 %then %do;

          proc datasets lib=work;

          run;

     %end;

%mend;

%execute;

Frequent Contributor
Posts: 137

Re: How to condtionally execute Proc Datasets procedure?

Nice, Please correct my understanding , I'd have to store those macrovariable as global and then call them in the %execute macro definition?Also, If there are some sas processes before and after the macro execute, I hope the ones that are after will only execute after the execution of %macro execute coz that is what is most important in my logic. In essence, deleting the dataset should terminate the process is my need. Thanks

Solution
‎06-29-2015 06:21 AM
Respected Advisor
Posts: 4,138

Re: How to condtionally execute Proc Datasets procedure?

Proc Datasets doesn't abort/kill a process. It also depends what you mean by "process". Is this a separate SAS program or are you only talking about skipping certain steps within a single program (a single process).

To skip steps in a program the easiest way is to wrap everything into a macro and then use a %GOTO statement SAS(R) 9.4 Macro Language: Reference, Third Edition

macro wrapper();

  Data process;

/*    k=24;*/

    k=25;

    call symputx('k',k);

  run;

  %if &k=25 %then %goto exit;

  data _null_;

    put "Only executes if condition false";

    stop;

  run;

  %exit:

  data _null_;

    put "executes always";

    stop;

  run;

%mend;

%wrapper()

Frequent Contributor
Posts: 137

Re: How to condtionally execute Proc Datasets procedure?

Yes skipping steps based on a condition:

Let me give a bit more info and forgive me that I didn't do that fully in my question.

/*for checking*/

clientid into macrovar1

row_count into macrovar2

nobs into macrovar3

/* Logic*/

If &macvar2  ne &macrovar3   and missing(macrovar1) then stop the process right there and notify the sender

else continue;

Thanks,

Charlotte

Respected Advisor
Posts: 4,138

Re: How to condtionally execute Proc Datasets procedure?

You can use "my" wrapper approach. That will work in all cases. Else: It depends if you want to run the process in batch or online (using something like "abort" or "endsas").

...but as you anyway will need some macro logic to test your macro variables: I would recommend to use the wrapper approach as already posted.

%macro wrapper();

  Data process;

/*    k=24;*/

    k=25;

    call symputx('k',k);

  run;

  /*exception handling */

  %if &k=25 %then

    %do;

      <send notification>

      %goto &exit;

    %end;

  data _null_;

    put "Only executes if condition false";

    stop;

  run;

  %exit:

%mend;

%wrapper()

Frequent Contributor
Posts: 137

Re: How to condtionally execute Proc Datasets procedure?

Thank you Patrick and Xia. Such ideas make merry. Have a great working week ahead- Cheers!

Super User
Posts: 9,878

Re: How to condtionally execute Proc Datasets procedure?

Charlotte,

Not quite understand you totally . But call execute might give you some help.

Data process;

k=25;

call execute ('proc datasets lib=work;');

if var1 ne var2 or missing(K) then call execute(' delete process; ');

call execute('quit;');

run;

Xia Keshan

Contributor
Posts: 21

Re: How to condtionally execute Proc Datasets procedure?

Hi Charlotte,

From your second post it is not clear that what is difference between row_count and nobs but I try to do something :

%macro wrapper();

  %global macrovar1 macrovar2 macrovar3;

  proc sql noprint;

     select count(*) into :macrovar2 from talent3;

data newprocess;

  set talent3 nobs = tot;

  macrovar1 = 25;

  call symput('macrovar1', trim(put(macrovar1, 3.)));

  call symput('macrovar3', put(tot, 4.));

  run;

  data _null_;

  /*exception hanling*/

  %if &macrovar2 ne &macrovar3 and &macrovar1 = %then

  %do;

       %goto exit;

  %end;

  %else

  %do;

       call execute('proc print data = talent3;');

       call execute('run;');

  %end;

  run;

%exit: %mend;

%wrapper;

Br,Amit

🔒 This topic is solved and locked.

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

Discussion stats
  • 8 replies
  • 294 views
  • 6 likes
  • 5 in conversation