BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
hellohere
Pyrite | Level 9
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.
1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User
%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.

View solution in original post

4 REPLIES 4
pink_poodle
Barite | Level 11
Yes, this can be done by accessing dictionary tables with proc sql.
Tom
Super User Tom
Super User

@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;
Kurt_Bremser
Super User
%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.

AllanBowe
Barite | Level 11

Is this the macro (function) you are looking for?

 

https://core.sasjs.io/mf__existvar_8sas.html

/Allan
SAS Challenges - SASensei
MacroCore library for app developers
SAS networking events (BeLux, Germany, UK&I)

Data Workflows, Data Contracts, Data Lineage, Drag & drop excel EUCs to SAS 9 & Viya - Data Controller
DevOps and AppDev on SAS 9 / Viya / Base SAS - SASjs

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 1365 views
  • 3 likes
  • 5 in conversation