How to get current table name inside sas data step? [duplicate] - sas

This question already has answers here:
SAS: concatenate different datasets while keeping the individual data table names
(2 answers)
Closed 1 year ago.
I need to join some tables and create a new column with some mark to indicate the table that data came from.
I have tbl_202101, tbl_202102 ... tbl_202109. I need to join all, adding a new column table_origin, for example, indicating the respective table.
DATA FINAL_TABLE;
SET TBL_202101 - TBL_202109;
/* Here I don't know how to identify the current table */
table_origin = CASE
WHEN *CURRENT TABLE* = TBL_202101 THEN 202101
WHEN *CURRENT TABLE* = TBL_202102 THEN 202102
AND GO ON...
RUN;
How could I do it?

Set statement option
INDSNAME=variable
creates and names a variable that stores the name of the SAS data set
from which the current observation is read. The stored name can be a data
set name or a physical name. The physical name is the name by which the
operating environment recognizes the file.

Related

What is the TRUNCATE TABLE command in QuestDB? [duplicate]

This question already has answers here:
Mysql: Which to use when: drop table, truncate table, delete from table
(5 answers)
Closed 4 months ago.
In the documentation it says
TRUNCATE TABLE is used to permanently delete the contents of a table without deleting the table itself.
Can anyone please give an example of it?
Unlike DROP TABLE, TRUNCATE TABLE keeps the table and deletes only all rows that are currently stored in the table. This means that you don't need to re-create the table, if you're planning to insert new rows into it once it's truncated.
This might be handy for local experiments (but not only) where you regenerate the data occasionally and run some queries over it.

Variable has never been referenced [SAS]

I have a dataset containing information by an account number. I'm trying to add a new variable, called product_type, populated with the same value for every record. It is within a SAS macro.
data CC_database;
set cc_base_v2 (keep=accnum date product_type);
product_type="CC";
where date>=%sysevalf("&start_date."d) and file_date<=%sysevalf("&end_date."d);
run;
However, I keep getting the error "The variable product_type has in the DROP, KEEP, or RENAME list has never been referenced" and the output dataset only shows a blank column called product_type. What is happening here?
You are using the KEEP= dataset option on the input dataset. So the error is saying PRODUCT_TYPE does not exist in the dataset CC_BASE_V2.
If you want to control what variables are written by the data step just use a KEEP statement.
keep accnum date product_type;
In your case you could use the KEEP= dataset option but only list the variables taht are coming from CC_BASE_V2.
data CC_database;
set cc_base_v2(keep=accnum date file_date);
where date>= "&start_date."d and file_date<="&end_date."d;
product_type="CC";
drop file_date;
run;

Difficulty understanding the "_n_" variable in SAS, and how it applies to a concatenate function

I am very new to SAS, and for whatever reason am finding a lot of difficulty deciphering what this code block (below) does. I've googled and search stackoverflow to no avail. I'd appreciate any input, thanks!
set dataset;
id=cat("L",_n_);
run;
Probably there must be a data statement as well.
data newdataset;
set dataset;
id = cat("L", _n_);
run;
This above code creates a new dataset named newdataset from the existing dataset named dataset.
Also creating a new column called id, and id is creating by concatenating a constant character value "L" with the automatic variable _n_ using the CAT function. The automatic variable _n_ represents the number of times the DATA step has iterated.

SAS issue with Rename variable

I have a dataset with 6 character variables including Day5,Day6,Day7,City1,City2,City3.
I am trying to rename Day5 which was extracted as i__Day5 after importing txt file into SAS. The variable i__day5 is not getting renamed to day5 and so it does not shows any observation for this variable.
data subset ;
set subset ;
rename i__Day5 = Day5;
run;
Thanks.
As Tom mentioned your problem likely stems from overwriting the original table with the modified data, and then trying to submit your code to run again.
It will work the first time when the variable i__Day5 exists, but on running it a second time, the variable will no longer exist as it has already been renamed.
To avoid this issue never re-use table names. This code would be better:
data subset2 ;
set subset ;
rename i__Day5 = Day5;
run;
Space is cheap so there's no real downside to doing this, plus it gives you an easy way to compare the table before/after running the code.
The only other issue that this could be is that you are viewing field labels and not field names. As samkart mentions, you can verify the actual field names by running a proc contents against your table.

Base SAS programming

My dataset consists a variable named as "REPORTING_ENTITY" in the form of string. Now, from that variable i want create a new dataset which consist a observations with keywords as ('Bank','Loan','Cooperative','SBI','Insurance') from "REPORTING_ENTITY" also want that strings get deleted from original dataset. I did something like:
data class.sample;
set work.sample;
where REPORTING_ENTITY contains ('Bank','Loan','Cooperative','SBI','Insurance');
run;
This will create a new dataset with mentioned keywords but it will not get deleted from orignal dataset..
One way to do this is to overwrite the input dataset while simultaneously creating a new output dataset:
data class.sample
work.sample;
set work.sample;
if REPORTING_ENTITY in ('Bank','Loan','Cooperative','SBI','Insurance')
then output class.sample;
else output work.sample;
run;