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

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Update

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
  • 1090 views
  • 0 likes
  • 3 in conversation