%let go_on=no;
proc sql noprint;
select "yes" into :go_on
from dictionary.columns
where libname = "LIBRARY" and memname = "DATASET" and upcase(name) = "VARIABLE";
quit;
%if &go_on. = yes
%then %do;
/* code */
%end;
If the SQL does not find a matching entry, the macro variable is left untouched.
@hellohere wrote:
How to tell whether dataset contains a specific column?! Any way to do it by fetching meta info?! rather than go proc contents?! Need a macro to tell whether dataset contains a specific column or not. If yes, delete column. If not, do nothing.
So you are going to delete it if it exits?
Sounds like you just need to check out the DKRICOND or DKROCOND option.
You can use that option to suppress any error (or even any warning) if you try to drop a variable that does not exist.
Consider this program:
data want;
set sashelp.class(drop=X);
run;
Example:
770 %let dkricond=%sysfunc(getoption(dkricond)); 771 %put &=dkricond; DKRICOND=ERROR 772 data want; 773 set sashelp.class(drop=X); ERROR: The variable X in the DROP, KEEP, or RENAME list has never been referenced. 774 run; NOTE: The SAS System stopped processing this step because of errors. WARNING: The data set WORK.WANT may be incomplete. When this step was stopped there were 0 observations and 0 variables. WARNING: Data set WORK.WANT was not replaced because this step was stopped. NOTE: DATA statement used (Total process time): real time 0.03 seconds cpu time 0.04 seconds 775 options dkricond=nowarn; 776 data want; 777 set sashelp.class(drop=X); 778 run; NOTE: There were 19 observations read from the data set SASHELP.CLASS. NOTE: The data set WORK.WANT has 19 observations and 5 variables. NOTE: DATA statement used (Total process time): real time 0.02 seconds cpu time 0.00 seconds 779 options dkricond=&dkricond;
%let go_on=no;
proc sql noprint;
select "yes" into :go_on
from dictionary.columns
where libname = "LIBRARY" and memname = "DATASET" and upcase(name) = "VARIABLE";
quit;
%if &go_on. = yes
%then %do;
/* code */
%end;
If the SQL does not find a matching entry, the macro variable is left untouched.
Is this the macro (function) you are looking for?
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.