The code below brings in many excel files from a folder (they all contain the same variables).
All the observations in the variable named "dataset" begin with "allinj" and are followed by numbers (eg allinj82614, allinj23014, allinj51614, etc)
I would like to append the files into one file and keep three variables (ie clm_num, doi, x).
Also, I need to add a variable into this new file that contains the variable dataset (eg allinj82614, allinj23014, allinj51614) so that it's known which excel file the data came from.
Does anyone know how to do this?
filename dir "C:\Users\Desktop\Test_Import\*.xls ";
data new;
length filename fname $ 100;
infile dir eof=last filename=fname;
input ;
last: filename=fname;
run;
data a;
set new;
call scan(filename,6,pos3,len,'\');
drop len;
filename1=substr(filename,pos3);
dataset=compress(filename1,"\-_.");
run;
data _null_;
set a;
call execute('proc import datafile="C:\Users\Desktop\Test_Import\'||strip(filename1)||'" out='||strip(dataset)||';
run;');
run;
If all the files contain the same variables then you can use the indsname option.
data all;
set allinj: indsname=src_file;
excel_file=src_file;
keep clm_num doi x;
run;
If all the files contain the same variables then you can use the indsname option.
data all;
set allinj: indsname=src_file;
excel_file=src_file;
keep clm_num doi x;
run;
Hi Reeza,
Thank you.
Do you also know how to add a variable into this new file that contains the variable "dataset" (eg allinj82614, allinj23014, allinj51614) so that it's clear which excel file the data came from?
Just add excel_file to the keep statement in Reeza's code.
PG
Sir.if possible explain with a detailed example it would help me.I am a beginner.
ADD to KEEP STATMENT:
data all;
set allinj: indsname=src_file;
excel_file=src_file;
keep clm_num doi x excel;
run;
The kind of documentation that would always be required.:
/* Create a dataset called all */
data all;
/* Read all datasets with names starting with allinj. Create a variable called src_file that will hold the actual name of the dataset being read. */
set allinj: indsname=src_file;
/* Copy the name of the dataset being read to a variable called excel_file */
excel_file=src_file;
/* List the names of variables which will be part of the all dataset */
keep clm_num doi x excel_file;
run;
PG
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.