DATA Step, Macro, Functions and more

Data Set options

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 10
Accepted Solution

Data Set options

[ Edited ]

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;


Accepted Solutions
Solution
‎04-21-2017 10:03 AM
Super User
Posts: 10,020

Re: Data Set options

Posted in reply to Melvin_Sas



data b;
 set sashelp.class;
 stop;
run;

data a;
 set sashelp.class;
run;

data a(repempty=no);
 set b;
run;

View solution in original post


All Replies
Super User
Posts: 5,498

Re: Data Set options

Posted in reply to Melvin_Sas

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)

Respected Advisor
Posts: 3,156

Re: Data Set options

Posted in reply to Melvin_Sas

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.

Super User
Super User
Posts: 7,039

Re: Data Set options

[ Edited ]
Posted in reply to Melvin_Sas

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.
PROC Star
Posts: 1,759

Re: Data Set options

[ Edited ]
Posted in reply to Melvin_Sas

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).

 

 

Solution
‎04-21-2017 10:03 AM
Super User
Posts: 10,020

Re: Data Set options

Posted in reply to Melvin_Sas



data b;
 set sashelp.class;
 stop;
run;

data a;
 set sashelp.class;
run;

data a(repempty=no);
 set b;
run;
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 286 views
  • 9 likes
  • 6 in conversation