BookmarkSubscribeRSS Feed
marksanter
Fluorite | Level 6

Hello,

 

I have a few hundred filenames that look like:

 

1234A_file.xls

ABCDB_file.xls

CATSA_file.xls

 

I would like to take the first four characters to create a variable and the fifth character to create a separate variables at the beginning of my dataset. The dataset would look something like what is attached. 

 

I would greatly appreciate any guidance on how to achieve this.  

 

Mark

5 REPLIES 5
Reeza
Super User
From the variable values that's straightforward.

var1 = substr(variable, 1, 4);
var2 = substr(variable, 5, 1);

marksanter
Fluorite | Level 6

Thank you for your help, will this pull the variable names from the file name itself? The current spreadsheets do not include the variables I need - those are found in the file names.  

 

Thank you,

Mark 

Reeza
Super User

I would assume you're capturing the filenames when importing your data. If you're not, then that is a different question and the answer depends on how you're importing your data. 

 

I don't think you explained your problem fully...

Tom
Super User Tom
Super User

Much harder to do with EXCEL files than it would be with text files.  When reading text files you can use the FILENAME= option on the INFILE statement to access the name of the file being read.

 

You will need to use macro variable(s).

%let fname=1234A_file.xls;
proc import datafile="&fname" out=step1 replace dbsm=xls ;
run;
data want ;
   var1 = "%substr(&fname,1,5)";
   var2 = substr(var1,5,1);
   set step1;
run;
AMSAS
SAS Super FREQ

Not sure if this is what you are looking for. The code below goes out and grabs all the files in a directory and creates variable from the file names. I included a link to the External File Functions.

 

/*

External File Functions
https://go.documentation.sas.com/?cdcId=pgmsascdc&cdcVersion=9.4_3.5&docsetId=allprodsle&docsetTarget=syntaxByCategory-function.htm&locale=en#n1xjxmfjlyclzjn1hyatp8m76hr7

*/

filename myDir "\Some\folder\some\where" ;

data want ;
 	/* get a directory handle for the directory */
	dirRC=dopen("myDir") ;
	put dirRC= ;
	/* get the number of files in the directory */
	dNum=dnum(dirRC) ;
	put dNum= ;
	/* loop through the files */
	do i=1 to dnum ;
		/* get the filename */
		filename=dread(dirRC,i) ;
		put filename= ;
		/* now do whatever you want with the filename */
		var1=substr(filename,1,4) ;
		output ;
	end ;
run ;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 5 replies
  • 1035 views
  • 0 likes
  • 4 in conversation