DATA Step, Macro, Functions and more

Merge in= value comparison

Reply
Super Contributor
Posts: 435

Merge in= value comparison

What is the equivalent SAS code for the code below? Any help is greatly appreciated.

data hh_prods;

merge nodups_hhlevel_data (in=a) dups_hhlevel_data(in=b);

by name1 ADDRESS zip account_id;

if a.prod = b.prod then

prod_code=1;

else prod_code+1;

run;

When I ran this code I got error as follows. Ideally I need to compare values of the variable 'prod' in both the tables before generating the variable 'prod_code'.

24         data hh_prods;

25         merge nodups_hhlevel_data (in=a) dups_hhlevel_data(in=b);

26         by name1 ADDRESS zip account_id;

27         if a.prod = b.prod then

              ___________

              557

ERROR: DATA STEP Component Object failure.  Aborted during the COMPILATION phase.

ERROR 557-185: Variable a is not an object.

Super User
Super User
Posts: 7,942

Re: Merge in= value comparison

That is SAS code.  You cannot use a.variable notation like that, also a dataset will throw warnings if same variables appear in both inputs.  I would change your code to look like:

data hh_prods;

     merge nodups_hhlevel_data dups_hhlevel_data (rename=(prod=b_prod));

     by name1 ADDRESS zip account_id;

     if prod = b_prod then prod_code=1;

     else prod_code+1;

run;

Super User
Posts: 7,768

Re: Merge in= value comparison

The x.y notation in the data step is used for objects (eg the hash object). No object a or b has been declared in this data step (that's why you get the error message), but you have the two variables a and b which will hold the value of 1 (true) if a matching observation is present from the respective dataset.

Note that if the variable prod is present in both datasets, it will exist only ONCE in the data step and be filled with the most recently read value from either dataset. Making distinctions like a.prod or b.prod is SQL syntax only valid in proc sql when you assign the aliases a and b to input datasets.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 30

Re: Merge in= value comparison

Hi ,

you can also use below code .

data hh_prods;

merge nodups_hhlevel_data (in=a) dups_hhlevel_data(in=b);

by name1 ADDRESS zip account_id;

if a and b then prod_code=1;

else prod_code+1;

run;

Ask a Question
Discussion stats
  • 3 replies
  • 227 views
  • 6 likes
  • 4 in conversation