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

Hi,

 

suppose that I have 3 tables: A,B,C.

 

What I would like to do is for each table to add a new column called "table" and the value in each row of this new column for each table is the name of the table itself.

 

So table A will have a new column "table" with values a, a, a, a, ....

table B will have a new column named "table" with values b,b,b,...

And so on.

 

Thank you ! 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
I did put the answer above, under the spoiler tag on how you could do it via a macro. The solution below is what you really want though, which is what I suspected.

data want;
set stock1 stock2 stock3 stock4 indsname=source;
table=scan(source, 2);
run;

View solution in original post

6 REPLIES 6
Reeza
Super User

Why would you want to do this?

ilikesas
Barite | Level 11

Hi Reeza,

 

I have many tables of stocks. Each table is named after the stock and has two columns: "date" and "daily price".

 

In order to perform my analysis I need to merge all the tables together into one table, but since there is no indicator which date and price is for which stock, the analysis won't be possible.

 

 

Thank you!

Reeza
Super User
I did put the answer above, under the spoiler tag on how you could do it via a macro. The solution below is what you really want though, which is what I suspected.

data want;
set stock1 stock2 stock3 stock4 indsname=source;
table=scan(source, 2);
run;
ilikesas
Barite | Level 11

Thank you Reeza,

 

I got the answer that I needed!!!

 

Indsname happens to be a very useful function, I didn't even know it existed.

 

Thank you again!!!

lissacoffey
Calcite | Level 5

The solution above is what i really wanted

Reeza
Super User

Take advantage of the INDSNAME option to automatically grab the table name. 

I'm really curious for the use case of adding this variable, besides when you are setting multiple tables together and even then you can use the INDSNAME option directly. 

 

 

 

%macro add_name(dsn);

data &dsn;
set &dsn indsname=source;
Table = scan(source, 2);
run;

%mend;

%add_name(tableA);
%add_name(TableB) 

 

sas-innovate-2024.png

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.

 

Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 6 replies
  • 1658 views
  • 1 like
  • 3 in conversation