Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 444
  • Last Modified:

Combining Data Sets

I'm combining data sets in a program, but sometimes one or more of the data sets have zero records so when the program runs, it errors on that step because that data set may not exist.  Is there a macro or if statement that can be used so that the program would continue?

So my combine SAS step would look like this:

data MY_COMBINED_DATA;
set    THE_FIRST_DATASET
         THE_SECOND_DATASET
         THE_THIRD_DATASET
         THE_FOURTH_DATASET
         THE_FIFTH_DATASET;
run;

Let's say the step that creates THE_SECOND_DATASET produces zero records?  When it gets to this step, obviously it errors because that data set does not exist.  How can I get by this???
0
Lee R Liddick Jr
Asked:
Lee R Liddick Jr
  • 3
  • 2
1 Solution
 
Aloysius LowCommented:
you can put the data step within a macro and use the sysfunc and exist macro functions:

%macro combine_data;
data MY_COMBINED_DATA;
set    
%if (%sysfunc(exist([lib].THE_FIRST_DATASET)) eq 1) %then %do;
THE_FIRST_DATASET
%end;
%if (%sysfunc(exist([lib].THE_SECOND_DATASET)) eq 1) %then %do;
THE_SECOND_DATASET
%end;
%if (%sysfunc(exist([lib].THE_THIRD_DATASET)) eq 1) %then %do;
THE_THIRD_DATASET
%end;
%if (%sysfunc(exist([lib].THE_FOURTH_DATASET)) eq 1) %then %do;
THE_FOURTH_DATASET
%end;
%if (%sysfunc(exist([lib].THE_FIFTH_DATASET)) eq 1) %then %do;
THE_FIFTH_DATASET
%end;
;
run;
%mend;

%combine_data; /*execute the macro*/
0
 
Lee R Liddick JrReporting AnalystAuthor Commented:
If I run this with all the tables present, the MY_COMBINED_DATA dataset has 0 observations.  When I run this and one or more of the datasets are not present, it gives me an error:

ERROR:  File Work.MY_COMBINED_DATA.DATA does not exist.
0
 
Aloysius LowCommented:
did you replace [lib] with the appropriate library?

i have tested the code and it's working properly
0
 
Lee R Liddick JrReporting AnalystAuthor Commented:
I'm an idiot, no I didn't. Works fine now.
0
 
Aloysius LowCommented:
no worries, glad to know it's working :)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now