Hi I have written som code to go through a table of tablenames and take each tablename and put it in to a sql statement in a macro. There are 2 table names in the table the first table name is read in and put into the WHERE clause and according to debug the value does change to the 2nd table name but the macro always keeps the first table name. Can you help me figure out why?
Here is the data step:
data _null_/debug;
set dbtblnames_complete;
%put &newtblname1;
%let crnttblname = &newtblname1;
%put &crnttblname;
call execute('%nrstr(%%tstuniqvalid(&newtblname1));');
run;
Here is the macro that gets called:
%macro tstuniqvalid(crnttblname);
proc sql;
connect to teradata as eiwp (username=&xxxuser. password=&xxxpass. tdpid=&xxxx_TDPID. mode=teradata fastload=yes);
create table unique_validcustid as
select count(*) from connection to eiwp (
select hist_ky_add
,count(*)
from (select cast(cust_id as decimal(15,0)) as cust_id_test
,cust_id
,cust_id_type
,cell_ky
,rank(cust_id || cust_id_type || cast(cell_ky as varchar(16))asc)as hist_ky_add
from &crnttblname) a
group by 1
having count(*) > 1
);
%put "SQL Return code and message" &sqlxrc &SQLXMSG;
%mend;
According to the debug the variable newtblname1 is getting changed
> go
newtblname1 = dbname.table1
Old value =
Value changed at line 8964 column 5
Stepped to line 8968 column 5
> go
newtblname1 = dbname.table2
Old value = dbname.table1
Value changed at line 8964 column 5
> go
newtblname1 =
Old value = dbname.table2
Value changed at line 8964 column 5
> go
but each iteration has the first table name (dbname.table1) only.
I can't figure out why. Please take a look and advise. Thank you
%LET is not a data step statement.
Assuming crnttblname is the variable in dbtblnames_complete data set
concatenate it in the call execute function:
data _null_/debug;
set dbtblnames_complete;
call execute('%nrstr(%%tstuniqvalid(' || strip(crnttblname) || '));');
run;
Try even:
call execute('%tstuniqvalid(' || strip(crnttblname) || ');');
You seem confused about the order of execution of macro code, data steps and code pushed to be executed by CALL EXECUTE. Also since this data step is not doing anything with any data why would you want to try to use the data step debugger with it?
Since the data step is not doing anything with the data from dbtblnames_complete the only impact that dataset will have how many times that the data step will iterate. So if it has 3 observations then your first data step is essentially running this.
%put &newtblname1;
%let crnttblname = &newtblname1;
%put &crnttblname;
data _null_;
call execute('%nrstr(%%tstuniqvalid(&newtblname1));');
call execute('%nrstr(%%tstuniqvalid(&newtblname1));');
call execute('%nrstr(%%tstuniqvalid(&newtblname1));');
run;
Which will result in these three statements being submitted to run after the DATA _NULL_ step finishes.
%%tstuniqvalid(&newtblname1);
%%tstuniqvalid(&newtblname1);
%%tstuniqvalid(&newtblname1);
Not sure what the impact is of the extra % character there.
Thanks i will take those courses, right now I am wrapping up the getting started programming course for sas
Thanks you I am new to SAS programming
%LET is not a data step statement.
Assuming crnttblname is the variable in dbtblnames_complete data set
concatenate it in the call execute function:
data _null_/debug;
set dbtblnames_complete;
call execute('%nrstr(%%tstuniqvalid(' || strip(crnttblname) || '));');
run;
Try even:
call execute('%tstuniqvalid(' || strip(crnttblname) || ');');
Thank you! concatenating it worked: call execute('%tstuniqvalid(' || strip(crnttblname) || ');');. I do not really understand why this works yet.
Call Execute argument is a string to submit.
Suppose cmttblname = "Table1" then the submitted string is
%tstuniqvalid(Table1);
This:
data _null_/debug;
set dbtblnames_complete;
%put &newtblname1;
%let crnttblname = &newtblname1;
%put &crnttblname;
call execute('%nrstr(%%tstuniqvalid(&newtblname1));');
run;
is equivalent to this:
%put &newtblname1;
%let crnttblname = &newtblname1;
%put &crnttblname;
data _null_/debug;
set dbtblnames_complete;
call execute('%nrstr(%%tstuniqvalid(&newtblname1));');
run;
as the macro statements are resolved while the code is being fetched for the data step compiler.
Since &newtblname1 never changes, all your macro calls will receive the same parameter.
thanks , still a bit fuzzy though
It looks like you are trying to run a query in Teradata for each table/dataset name that you have in some dataset?
You seem to be generating a single number from this query?
Is the goal to start with a dataset like this:
data have ;
input tablename :$32. ;
cards;
table1
table2
table3
;
And generate a table like this?
data want;
input tablename :$32. count ;
cards;
table1 10
table2 0
table3 23456
;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.