BookmarkSubscribeRSS Feed
jmono
Calcite | Level 5

I've been using SAS for only 2 years and my next task is rather a complicated one.

 

Summary of the task: I need to export two columns from multiple excel files (xlsx; ~120 files) into one dataset. Each excel file consititute a record or a case. When imported, I would like for the columns from each record to be a row so that in the new dataset, I'll have each record on a row. I understand that for the latter step, I'll need to transpose.

 

Steps taken: I know how to import one entire excel file into a SAS dataset. I've looked at articles but they only discussed importing select columns or rows from one excel file -- NOT from multiple files. 

 

What I think needs to be done

  1. Write a code to reference the folder where the excel files are stored.
  2. Write a code to extract the two columns of interest from the first excel file and store into a new dataset.
  3. Then reiterate this code to continue extracting from the next files until it gets to the last file.
  4. Make sure to flag those files that have been imported.
  5. The new dataset will now contain the following columns: ID-1, Code-1, ID-2, Code-2, ID-3, Code-3, ... Therefore, next step is to transpose.

What I'm avoiding (marked with *):

  1. Extract both columns of interest from each excel file into a dataset. This will create ~120 datasets*.
  2. Transpose each of the new datasets. 
  3. Merge all new datasets. 
  4. Delete the ~120 datasets.

Any coding tips will be helpful, and necessary tweaks to my thinking processes will be appreciated. 

 

I'm currently using SAS Studio at home, the free version, which makes things a little bit more challenging. 

 

Thanks!

1 REPLY 1
Reeza
Super User

Essentially you're looking for a macro.

 

For importing the data from Excel, are the values always in the same location? You can specify the range in Proc Import. Do you have a list of the files or do you need to generate it?

 

Get it working for one file and then move on. I disagree with your last set of arguments, its probably easier to import each file, transpose and append to the final file. Make sure to clean up between loops. 

 

Here's a rough sketch to get you started. Once you get it working, wrap it in %macro/%mend and then you can use CALL EXECUTE to iterate over all your files. 

 

 

%let file2imp=myfile.xlsx;

proc import out=temp1 datafile="/folders/myfolders/&file2imp" dbms=excel replace; range='Sheet1$A4:G4'; run;

*Transpose here;
proc transpose data=temp1 out=temp2;
by id;
run;

*Append to table;
proc append base=want data=temp2;
run;

*clean up;
proc sql;
drop table temp1, temp2;
quit;

*repeat;

 

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
  • 1 reply
  • 1930 views
  • 0 likes
  • 2 in conversation