BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
ChickenLittle
Obsidian | Level 7

I have a table that contains a field named TableName, which stores the name of the specific table I need to pull from for each observation (re: each observation requires me to hit against different tables to grab NeededField). For each observation, is there a way to code it so that depending on the table name in the TableName field, I can pull the field I need just from that table for that particular observation?

 

Example:

libname abc 'path/of/my/file';

 

OriginalTable

obs    key     TableName

1          cat     abc.202201   

2         dog    abc.202202

3         frog    abc.202303

 

Table at abc.202201

key        NeededField

cat         1234

dog        12345

frog        123456

 

Table at abc.202202

key        NeededField

cat         345

dog        3456

frog        34567

 

Table at abc.202202

key        NeededField

cat         5

dog        56

frog       567

 

I want to see:

OriginalTable

obs    key     TableName      NeededField

1          cat     abc.202201      1234

2         dog    abc.202202      3456

3         frog    abc.202303      567

 

Is this at all possible? Thank you in advance.

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Doable, yes, efficient, probably not.

 

One option: combine all into one table with the source table identified using INDSNAME option. Then merge for desired results on the two key variables. You could limit to just key values needed if size might be an issue.

 

data combined;
set abc.202201-abc.202203 indsname =src;
tableName=src;
keep key NeededField tableName;
run;

proc sql;
create table want as
select a.*, b.needed_field
from have as a
left join combined as b
on upcase(a.TableName)=upcase(b.TableName)
order by 1;
quit;

@ChickenLittle wrote:

I have a table that contains a field named TableName, which stores the name of the specific table I need to pull from for each observation (re: each observation requires me to hit against different tables to grab NeededField). For each observation, is there a way to code it so that depending on the table name in the TableName field, I can pull the field I need just from that table for that particular observation?

 

Example:

libname abc 'path/of/my/file';

 

OriginalTable

obs    key     TableName

1          cat     abc.202201   

2         dog    abc.202202

3         frog    abc.202303

 

Table at abc.202201

key        NeededField

cat         1234

dog        12345

frog        123456

 

Table at abc.202202

key        NeededField

cat         345

dog        3456

frog        34567

 

Table at abc.202202

key        NeededField

cat         5

dog        56

frog       567

 

I want to see:

OriginalTable

obs    key     TableName      NeededField

1          cat     abc.202201      1234

2         dog    abc.202202      3456

3         frog    abc.202303      567

 

Is this at all possible? Thank you in advance.


 

View solution in original post

6 REPLIES 6
ballardw
Super User

The table names you show are not normally valid SAS data set names. So are those "tables" SAS data sets or something in an external data system?

 

Basically you are going to  use the information you have write additional syntax. Possible? yes. Short and sweet? Maybe not.

Is the Key that you show going to be unique in each of the tables? If the key is not unique there will need to be additional rules provided on how to select the specific observation from each external data set.

ChickenLittle
Obsidian | Level 7

All data tables are sas data sets and the key is unique.

SASKiwi
PROC Star

How many tables, columns and rows are in your real use case? This is a really inefficient methodology for locating and reading your required data and is unlikely to scale well to large numbers of tables, columns and rows. If you explained your problem statement, there may be better approaches. 

Reeza
Super User

Doable, yes, efficient, probably not.

 

One option: combine all into one table with the source table identified using INDSNAME option. Then merge for desired results on the two key variables. You could limit to just key values needed if size might be an issue.

 

data combined;
set abc.202201-abc.202203 indsname =src;
tableName=src;
keep key NeededField tableName;
run;

proc sql;
create table want as
select a.*, b.needed_field
from have as a
left join combined as b
on upcase(a.TableName)=upcase(b.TableName)
order by 1;
quit;

@ChickenLittle wrote:

I have a table that contains a field named TableName, which stores the name of the specific table I need to pull from for each observation (re: each observation requires me to hit against different tables to grab NeededField). For each observation, is there a way to code it so that depending on the table name in the TableName field, I can pull the field I need just from that table for that particular observation?

 

Example:

libname abc 'path/of/my/file';

 

OriginalTable

obs    key     TableName

1          cat     abc.202201   

2         dog    abc.202202

3         frog    abc.202303

 

Table at abc.202201

key        NeededField

cat         1234

dog        12345

frog        123456

 

Table at abc.202202

key        NeededField

cat         345

dog        3456

frog        34567

 

Table at abc.202202

key        NeededField

cat         5

dog        56

frog       567

 

I want to see:

OriginalTable

obs    key     TableName      NeededField

1          cat     abc.202201      1234

2         dog    abc.202202      3456

3         frog    abc.202303      567

 

Is this at all possible? Thank you in advance.


 

data_null__
Jade | Level 19

You could just make a single lookup table and do a KEYed set.  

 

data a;
   length tableName $16;
   tableName = 'abc.202201';
   input key $ field;
   cards;
cat         1234
dog        12345
frog        123456
;;;;
data b;
   length tableName $16;
   TableName = 'abc.202202';
   input key $ field;
   cards;
cat         345
dog        3456
frog        34567
;;;;
data c;
   length tableName $16;
   TableName = 'abc.202303';
   input key $ Field;
   cards;
cat         5
dog        56
frog       567
;;;;
   run;
data lookuptable(index=(idx1=(tableName key)/unique));
   set a b c;
   run;
data want;
   set have;
   set lookuptable key=idx1/unique;
   run;
proc print;
   run;

Capture.PNG

ChickenLittle
Obsidian | Level 7
Thank you so much for your response! This works too, but I used the other responder's suggestion first. This was very helpful!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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
  • 6 replies
  • 499 views
  • 6 likes
  • 5 in conversation