BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Melvin_Sas
Fluorite | Level 6

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User



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

5 REPLIES 5
Astounding
PROC Star

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)

Haikuo
Onyx | Level 15

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.

Tom
Super User Tom
Super User

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.
ChrisNZ
Tourmaline | Level 20

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

 

 

Ksharp
Super User



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

data a;
 set sashelp.class;
run;

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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
  • 5 replies
  • 1017 views
  • 9 likes
  • 6 in conversation