I like to copy data from one dataset to another example from dataset B to dataset A but if there are 0 zero records in dataset B it should not overwrite dataset A. Is there any where clause option I can add in the set option to check this. Thanks
Data A;
set B;
run;
data b;
set sashelp.class;
stop;
run;
data a;
set sashelp.class;
run;
data a(repempty=no);
set b;
run;
You might be able to piece something together that works some of the time, using only a DATA step. But the best solution would use macro language. For example:
%macro replace (in=, out=);
%local any_obs;
%let any_obs=N;
data _null_;
set ∈
call symput('any_obs', 'Y');
stop;
run;
%if &any_obs=Y %then %do;
data &out;
set ∈
run;
%end;
%mend replace;
%replace (in=B, out=A)
For your purpose, you could simply do:
data A;
if n>0 then
set B nobs=n;
else set A;
run;
However, there are things you need to be aware of:
1. All variables will show up regardless the source dataset is copied or not, only their contents will come in missing if not copied.
2. When B is empty, instead of doing nothing, A is copied again.
Here is a method you can use. The first data _null_ step will test if there are any observations in B and set a macro varible to either CANCEL or spaces. That way you can conditionally add the CANCEL keyword to the second data step's RUN statement.
data _null_;
call symputx('cancel',ifc(eof,'cancel',''));
stop;
set b end=eof;
run;
data a;
set b;
run &cancel;
Note that you will get a message in the log if you cancel the second step.
1304 data a; 1305 set b; 1306 run &cancel; WARNING: DATA step not executed at user's request.
I would do this thus:
data _null_;
set B (obs=1);
call execute('proc datasets lib=WORK noprint; age B A; run;');
run;
The second statement is only executed if an observation can be read in B.
Renaming with proc datasets is much faster than copying a whole data set all over again, and conserves whatever attributes table B had (sortedby flag, index, compression, etc).
data b;
set sashelp.class;
stop;
run;
data a;
set sashelp.class;
run;
data a(repempty=no);
set b;
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.