Hi !,
I am trying to find a way in SAS to evaluate whether a certain varname is a variable or a table.
Ideas ?,
let's asume I have the following examples:
data example1; Name = "Charles"; Surname = "Robinson"; run; %let example2 = 201906 201704;
How can I get that &example1 is a table somehow or check that &example2 is a variable ?
I will use that inside a macro as:
if (&example= table) {then X}
else if ((&example= variabe ){then Y );
else{Z}
Thank you so much in advanced for your time and consideration,
@carles wrote:
Yes, it is to pass it through a macro and just say whether it is a table or a variable.
It can be BOTH at the same time. See this example for library SASHELP:
proc sql;
select name label='Name', count(*) as count
from (
select distinct(upcase(name)) as name
from dictionary.columns
where libname = 'SASHELP'
union all
select distinct memname as name
from dictionary.tables
where libname = 'SASHELP'
)
group by name
having count > 1;
quit;
Result:
Name count ------------------------------------------ CLASS 2 COMPANY 2 FEEDER 2 HOLIDAY 2 TABLE 2 THICK 2
All these appear both as variable and dataset name.
Example2 can't be either, as it is not a valid SAS name.
If you have something that constitutes a valid SAS name, you can check it against dictionary.tables and dictionary.columns. Mind that this will only find occurrences in currently assigned libraries.
Yes, it is to pass it through a macro and just say whether it is a table or a variable.
So if the macro determines that it is the name of two tables, as well as the name of a variable in five other tables, will that work?
So @Kurt_Bremser has already provided all the tools you would need to use. Here is an overview.
Your program will need to have issued LIBNAME statements are all the folders that need to be searched. Otherwise, SAS has no way of knowing where to look to find tables and variables.
Query DICTIONARY.TABLES to find table names. Query DICTIONARY.COLUMNS to find variable names. In both cases, you need to modify the WHERE conditions in Kurt's examples to look for just the one name that you are hoping to categorize.
You still have to figure out how to proceed if the name is both a table name and a variable name.
@carles wrote:
Yes, it is to pass it through a macro and just say whether it is a table or a variable.
It can be BOTH at the same time. See this example for library SASHELP:
proc sql;
select name label='Name', count(*) as count
from (
select distinct(upcase(name)) as name
from dictionary.columns
where libname = 'SASHELP'
union all
select distinct memname as name
from dictionary.tables
where libname = 'SASHELP'
)
group by name
having count > 1;
quit;
Result:
Name count ------------------------------------------ CLASS 2 COMPANY 2 FEEDER 2 HOLIDAY 2 TABLE 2 THICK 2
All these appear both as variable and dataset name.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.