BookmarkSubscribeRSS Feed
Babloo
Rhodochrosite | Level 12

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.

3 REPLIES 3
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

Kurt_Bremser
Super User

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.

naveen20jan
Obsidian | Level 7

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;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 3 replies
  • 931 views
  • 6 likes
  • 4 in conversation