DATA Step, Macro, Functions and more

Replacing observations in a dataset with values from another dataset

Reply
Contributor
Posts: 28

Replacing observations in a dataset with values from another dataset

I have a dataset A that contains one observation and one variable (NMaxBlocks).  I created dataset C by joining datasetA with datasetB, and dataset B did not contain the variable NMaxBlocks.  Because B did not originally contain the variable NMaxBlocks, all observations in C that were originally in B now contain "." for the variable NMaxBlocks.  I want all observations in C that contain "." because they were originally part of B to contain the value for NMaxBlocks that originally appeared in A. (See attached exerpt of output from C- all "." in NMaxBlocks need to contain 4 from NMAxBlocks in the last observation in the dataset).  I know there is a simple way to do this, but I am not enough of an experienced SAS user to know how to do this yet.  Any ideas?


Capture.PNG
Occasional Contributor
Posts: 10

Re: Replacing observations in a dataset with values from another dataset

There are several ways to accomplish this, here's one using multiple SET statements within a DATA step.

DATA WORK.C ;

  IF _N_ = 1 THEN SET WORK.A ;

  SET WORK.B  ;

RUN ;

Super User
Posts: 5,495

Re: Replacing observations in a dataset with values from another dataset

Perhaps you would be better off learning how to combine A and B, rather than fixing the problem later.  If you are using a DATA step, it would be:

data C;

   if _n_=1 then set A;

   set B;

run;

If you are using SQL:

proc sql noprint;

   create table C as select * from A, B;

quit;

You would no longer have that single observation at the end, where the contents of A matches nothing from B.

Good luck.

Ask a Question
Discussion stats
  • 2 replies
  • 189 views
  • 1 like
  • 3 in conversation