BookmarkSubscribeRSS Feed
Siddharth123
Obsidian | Level 7

Hi All,

 

I have a dataset with NAME column in it. I want to write a process where in I can test if, else if condition to say if the NAME does not fit in the desired list then Kill SAS procedure, else proceed.

 

I have coded like this -

 

%macro validate_c (data, cols);

proc contents  data= &data. out=cont_&data. (keep=  NAME) noprint; run;

 

PROC SQL NOPRINT;

SELECT * FROM cont_&data.

%if NAME NOT in (&cols.) %then %do;

      %PUT ABORT: NOT REQUIRED COLUMNS;

      %kill;

%end;

%else %if NAME in (&cols.) %then %do;

      %PUT PROCEED: REQUIRED COLUMNS;

%end;

QUIT;

%mend;

 

This gives me error -

ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand

       is required. The condition was: NAME NOT in (&cols.)

ERROR: The macro VALIDATE_C will stop executin

 

Please help.

 

Regards

9 REPLIES 9
Siddharth123
Obsidian | Level 7

%kill macro is -

 

%macro kill;

proc datasets lib = work

kill;

run;

%mend;

 

Reeza
Super User

What does the macro call look like?

Your PROC SQL looks incorrect, it ends in a period for one thing. It's also not doing anything...

Siddharth123
Obsidian | Level 7

%validate_c (Data, %str(var1 var2 var3));

gamotte
Rhodochrosite | Level 12

Hello,

 

Use of the IN operator in macros is conditionned by the MINOPERATOR system option.

http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/viewer.htm#a003092012.htm

Reeza
Super User

NAME is not a macro variable or variable that's been created anywhere either. It's literally looking for the value 'NAME'

Siddharth123
Obsidian | Level 7

I still get the same error.

 

Can this code be tested on dataset Country_Metadata_Refdata  as -

 

Name

E

Var1

Var2

Var3

 

and execution as -

 

%validate_c (Country_Metadata_RefData, %str(Var1, Var2, Var3));

This should kill SAS procecdure as column "E" does not exists in the execution

 

%validate_c (Country_Metadata_RefData, %str(Var1, Var2, Var3, E));

This should proceed without error as SAS as column "E" exists in the execution and variable List.

Reeza
Super User

Before writing a macro, you should have working base code that will terminate a process. 

 

What does that look like now?

 

Then you take your macro code and make it generate that code. So comparing what gets generated to your working code is the debug process. 

Tom
Super User Tom
Super User

You cannot "kill" a procedure. You can use macro logic to conditionally either generate the code that runs the procedure (or other SAS statements) or not.

If you are really want to just conditionally run select procedures then you migth be able to conditionally add the keyword CANCEL to the RUN statement. Could be as simple as setting a macro variable to either empty.

 

%let run_cancel=CANCEL;
proc print data=sashelp.class ;
run &run_cancel ;

Not sure if it works for all procedures and you will get a warning message in the log.

WARNING: The procedure was not executed at the user's request.

 

 

Tom
Super User Tom
Super User

You logic looks flawed. You appear to be trying to conditionally generate a call to PROC DATASETS inside the middle of an SQL statement.  You cannot call another proc while one is still running.

 

What do you want to do?  

It looks like perhaps you want to delete a dataset if it contains extra variables?

%macro validate_c (data, cols);
%local extra_vars ;
proc contents noprint data= &data. out=_contents ; 
run;

proc sql noprint ;
  select name into :extra_vars separated by ' '
  from _contents 
  where not indexw(upcase(name),%upcase("&cols"),' ')
  ;
quit;

%if &sqlobs %then %do;
  %put Found extra variables in &data ;
  %put &=extra_vars ;
  proc delete data=&data ;
  run;
%end;
proc delete data=_contents; run;
%mend validate_c;

So let's test it.

814   data test;
815    set sashelp.class ;
816   run;

NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: The data set WORK.TEST has 19 observations and 5 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.01 seconds


817   options mprint;
818   %validate_c(data=test,cols=Name Age);
MPRINT(VALIDATE_C):   proc contents noprint data= test out=_contents ;
MPRINT(VALIDATE_C):   run;

NOTE: The data set WORK._CONTENTS has 5 observations and 41 variables.
NOTE: PROCEDURE CONTENTS used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds


MPRINT(VALIDATE_C):   proc sql noprint ;
MPRINT(VALIDATE_C):   select name into :extra_vars separated by ' ' from _contents where not
indexw(upcase(name),"NAME AGE",' ') ;
MPRINT(VALIDATE_C):   quit;
NOTE: PROCEDURE SQL used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


Found extra variables in test
EXTRA_VARS=Age Height Name Sex Weight
MPRINT(VALIDATE_C):   proc delete data=test ;
MPRINT(VALIDATE_C):   run;

NOTE: Deleting WORK.TEST (memtype=DATA).
NOTE: PROCEDURE DELETE used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


MPRINT(VALIDATE_C):   proc delete data=_contents;
MPRINT(VALIDATE_C):   run;

NOTE: Deleting WORK._CONTENTS (memtype=DATA).
NOTE: PROCEDURE DELETE used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds

 

 

 

 

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
  • 9 replies
  • 1442 views
  • 5 likes
  • 4 in conversation