Help using Base SAS procedures

Merge fails

Reply
Occasional Contributor
Posts: 6

Merge fails

Absolute SAS and programming beginner. Stumped on what is wrong with this simple merge code:

data mydata.janfeb;

  merge mydata.jan2015 (IN=jan) 

            mydata.feb2015 (IN=feb);

            by acct_id;

            if jan=1 and feb=0 then refnum='no_match';

           else jan=1 and feb=1 refnum='match';

run;

The result is a log error right after the else jan=1 and feb=1 refnum='match'

                                 22

               ERROR 22-322: Syntax error, expecting one of the following: !, !!, &, *, **, +, -, /, ;, <, <=, <>, =, >, ><, >=, AND, EQ, GE, GT,

              IN, LE, LT, MAX, MIN, NE, NG, NL, NOTIN, OR, ^=, |, ||, ~=. 

I have proc sort step that runs without errors for both of the files before the data step.

I would appreciate any assistance.

Thank you.   

Super User
Posts: 10,500

Re: Merge fails

There are several problems with this line depending on what you may be attempting to do:

else jan=1 and feb=1 refnum='match';

If the intent was to assign a value to refnum you probably meant:

else if jan=1 and feb=1 then refnum='match';

One does wonder about Jan=0 and Feb=1 ?

You might want to think about coding your match / no match as numeric 1/0 for a number of reasons. One is that you can get a number of useful statistics from various procedures. You can use a custom format to have Match or No match displayed for text as needed.

Another is that you can assign a likely value using this code.

RefNum = (jan = feb); /* assuming that Jan=0 and Feb=1 is not a match*/

or

If Jan then RefNum = (jan = feb); /* assuming you only want to assign a value for the records from jan*/

PROC Star
Posts: 7,363

Re: Merge fails

I agree with Ballardw. Are you trying to do something like the following? (Note: I deleted your librefs, as I didn't want to create permanent files on my system):

data jan2015;

  input acct_id x $;

  cards;

1 a

2 b

3 c

4 d

5 e

;

data feb2015;

  input acct_id y $;

  cards;

2 B

3 C

4 D

;

data janfeb;

  merge jan2015 (IN=jan)

        feb2015 (IN=feb);

  length refnum $8;

  by acct_id;

  if jan and feb then refnum='match';

  else refnum='no_match';

run;

Occasional Contributor
Posts: 6

Re: Merge fails

Thank you all for your assistance and knowledge!!

I was trying to merge two files and be able to identify on the output which file contributed to the observation.  It turned out that I was missing the "if" following the "else" condition:

data mydata.janfeb;

  merge mydata.jan2015 (IN=jan) 

            mydata.feb2015 (IN=feb);

            by acct_id;

            if jan=1 and feb=0 then refnum='no_match';

           else IF jan=1 and feb=1 refnum='match';

Thanks all again. I am sure I will be back in this forum in the near future :-)

PROC Star
Posts: 7,363

Re: Merge fails

That shouldn't work either! If you include an "if", you'll also need a "then".  e.g.:

else IF jan=1 and feb=1 THEN refnum='match';


Ask a Question
Discussion stats
  • 4 replies
  • 270 views
  • 7 likes
  • 3 in conversation