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

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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;

View solution in original post

6 REPLIES 6
Reeza
Super User

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;

gzr2mz39
Quartz | Level 8

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?

PGStats
Opal | Level 21

Just add excel_file to the keep statement in Reeza's code.

PG

PG
venkatnaveen
Obsidian | Level 7

Sir.if possible explain with a detailed example it would help me.I am a beginner.

Reeza
Super User

ADD to KEEP STATMENT:

data all;

set allinj: indsname=src_file;

excel_file=src_file;

keep clm_num doi x excel;

run;

PGStats
Opal | Level 21

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

PG

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 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
  • 1468 views
  • 3 likes
  • 4 in conversation