04-20-2017 02:58 PM - edited 04-20-2017 03:16 PM
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
04-20-2017 03:35 PM
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=);
call symput('any_obs', 'Y');
%if &any_obs=Y %then %do;
%replace (in=B, out=A)
04-20-2017 05:01 PM
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.
04-20-2017 06:00 PM - edited 04-20-2017 06:05 PM
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.
04-20-2017 10:46 PM - edited 04-20-2017 10:57 PM
I would do this thus:
set B (obs=1);
call execute('proc datasets lib=WORK noprint; age B A; 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).
Need further help from the community? Please ask a new question.