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.
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;
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.
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;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.