BookmarkSubscribeRSS Feed
Anneliesant
Calcite | Level 5

Hi all,

 

SAS 9.4

 

I 'm trying to create a loop so that all the xlsx-files are imported. I notice that the code below is wrong but I don't know how to correct it the right way. I have 4 excelfiles to import. The result of this code is that the first excelfile is imported 4 times (and it overwrites itself each time). The result should be that 4 each excel file is imported once. Each excelfile has a different variable in its filename (ERK = 009, 090,091,777). Only the 009 is imported. Could anyone help me with this please?

 


/*macro to import a single excel*/
%macro import(input);

OPTIONS VALIDVARNAME=ANY;
PROC IMPORT OUT=work.B1B2_E&erk. (keep = N_BUD_AN ID_AGR TIT_N Source N2018)
DATAFILE= "&path\&input..xlsx"
DBMS=excelcs REPLACE;
RANGE="B1B2$"; /*to import the sheet completely*/
SCANTEXT=YES;
USEDATE=YES;
SCANTIME=YES;
RUN;

%mend;

/*loop to import all the files*/
%macro Loadall;
%do i=1 %to &numobs;
%import(&&filein&i);
%end;


%mend;

%loadall;

 

4 REPLIES 4
Kurt_Bremser
Super User

Since you do not change &erk in the loop (or derive it somehow from &input), the dataset is always overwritten with the data from the latest import.

Anneliesant
Calcite | Level 5

How do I write the code? 

 

Before the macro this is written. 

 


/*Each Excel file gets a variable, number*/
data _null_;
set mydata.deelnemerslijst end=endobs;/*end=variable --> at the last observation it starts counting*/
call symputx('fileIN'!!left(_n_),excel_file);/*takes the name and puts a number in front "n" (_n_ counts the lines --> this combination is named 'fileIN'*/
if endobs=1 then call symputx('numobs',_N_);
run;

RW9
Diamond | Level 26 RW9
Diamond | Level 26

There are many examples of this type on here, please use the search functionality:
https://communities.sas.com/t5/forums/searchpage/tab/message?advanced=false&allow_punctuation=false&...

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1078 views
  • 0 likes
  • 3 in conversation