BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
carles
Fluorite | Level 6


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,

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

@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.

View solution in original post

7 REPLIES 7
Kurt_Bremser
Super User

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.

Astounding
PROC Star
SAS allows the same variable name to appear in many tables.

It also allows the same name to be both the name of a table and the name of a variable.

These are common situations, not at all unusual.

Will your plan still work?
carles
Fluorite | Level 6

Yes, it is to pass it through a macro and just say whether it is a table or a variable.

Astounding
PROC Star

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?

Astounding
PROC Star

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.

 

 

Kurt_Bremser
Super User

@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.

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
  • 7 replies
  • 764 views
  • 6 likes
  • 3 in conversation