SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Toybeck11
Calcite | Level 5

Hello

 How do I write matched and non-matching observations from merged dataset to different new data sets

I am trying to write a single program where I get two data sets; one for the matched, 2nd for the unmatched. Having a problem with the nomatch08 data set.

 

Data results.08 (output=results.nomatch08);
Merge Books.input08a
(In=Input08a)
Books.input08b (In=Input08b);
By Id;
If Input08a=1 and Input08b=1; output;
run;

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
Data results.match08 
        results.nomatch08;


Merge Books.input08a  (In=Input08a)
          Books.input08b (In=Input08b);

By Id;

If Input08a and Input08b then output results.match08;
else output results.nomatch08;
run;

1. List both output data sets on the DATA statement - no parenthesis required

2. Add the OUTPUT to your IF statement and the name of the data set you want it written out to

3. Fix name of matched data set - names cannot start with a number (results.08 is not a valid SAS data set name).

 

 


@Toybeck11 wrote:

Hello

 How do I write matched and non-matching observations from merged dataset to different new data sets

I am trying to write a single program where I get two data sets; one for the matched, 2nd for the unmatched. Having a problem with the nomatch08 data set.

 

Data results.08 (output=results.nomatch08);
Merge Books.input08a
(In=Input08a)
Books.input08b (In=Input08b);
By Id;
If Input08a=1 and Input08b=1; output;
run;


 

View solution in original post

9 REPLIES 9
Reeza
Super User
Data results.match08 
        results.nomatch08;


Merge Books.input08a  (In=Input08a)
          Books.input08b (In=Input08b);

By Id;

If Input08a and Input08b then output results.match08;
else output results.nomatch08;
run;

1. List both output data sets on the DATA statement - no parenthesis required

2. Add the OUTPUT to your IF statement and the name of the data set you want it written out to

3. Fix name of matched data set - names cannot start with a number (results.08 is not a valid SAS data set name).

 

 


@Toybeck11 wrote:

Hello

 How do I write matched and non-matching observations from merged dataset to different new data sets

I am trying to write a single program where I get two data sets; one for the matched, 2nd for the unmatched. Having a problem with the nomatch08 data set.

 

Data results.08 (output=results.nomatch08);
Merge Books.input08a
(In=Input08a)
Books.input08b (In=Input08b);
By Id;
If Input08a=1 and Input08b=1; output;
run;


 

Toybeck11
Calcite | Level 5

Thanks a lot. It worked.

How do I exclude all variables that begin with "ex" from results.nomatch08? Same program?

Thanks for your help.

 

Data results.matcho8 results.nomatch08;
Merge
cert.Input08a (In=Input08a)
cert.Input08b (In=Input08b);
By Id;
If Input08a=1 and Input08b=1 then output results.matcho8;
else output results.nomatch08;
run;

Reeza
Super User
Only for one data set? Is there a naming convention besides just starting with ex?
Toybeck11
Calcite | Level 5
In one of the data sets results.match08, I want to drop all variables with "ex"
(Exclude all variables that begin with "ex" from results.nomatch08). the last statement is giving error.


Data results.match08 results.nomatch08;
Merge
cert.Input08a (In=Input08a)
cert.Input08b (In=Input08b);
By Id;
If Input08a=1 and Input08b=1 then output results.match08;
else output results.nomatch08; drop='ex:';
run;
Reeza
Super User
Is there a naming convention besides just starting with ex?

Your DROP statement is incorrect syntax wise.
You can use the data set options on the DATA statement but I'm not sure that takes wildcards, so if you have another naming convention you may be able to do that or otherwise you need to create that list ahead of time and there are ways. But if you have a naming convention that isn't needed.

So do you have a naming convention besides just ex?
Toybeck11
Calcite | Level 5
No I do not have a naming convention.
I'm working on a practice exam. this is all the question says:
a. Combine data sets cert.input08a and cert.input08b by matching values of the ID variable.
b. Write only observations that are in both data sets to a new data set named results.match08.
c. Write all other non-matching observations from either data set to a new data set named results.nomatch08.
o Exclude all variables that begin with "ex" from results.nomatch08.
Reeza
Super User

Here is a reference that illustrates how to refer to variables and datasets in a short cut list:
https://blogs.sas.com/content/iml/2018/05/29/6-easy-ways-to-specify-a-list-of-variables-in-sas.html

 

If none of those work for you then you'll have to dynamically generate the list but that's usually not requirement for the base exam, possibly the advanced one though. 

SB_15
Calcite | Level 5

Data results.match08 results.nomatch08;
Merge
cert.Input08a (In=Input08a)
cert.Input08b (In=Input08b);
By Id;
If Input08a=1 and Input08b=1 then output results.match08;
else output results.nomatch08;

drop=ex: ;
run;

pavana1
Calcite | Level 5

data rm nrm(drop=ex:);
merge dadw(in=a) famx(in=b);
by id;

if a and b then
output=rm;
else output=nrm;
run;

sas-innovate-white.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.

 

Early bird rate extended! Save $200 when you sign up by March 31.

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 9 replies
  • 12494 views
  • 8 likes
  • 4 in conversation