DATA Step, Macro, Functions and more

how to create loop inside macro

Reply
Contributor
Posts: 45

how to create loop inside macro

I have 10 file having name

a1_03/03/2011.csv
a2_04/03/2011.csv
a3_05/03/2011.csv
a4_06/03/2011.csv
a5_07/03/2011.csv
a6_08/03/2011.csv
a7_09/03/2011.csv
a8_10/03/2011.csv
a9_11/03/2011.csv
a10_12/03/2011.csv

Each file contain 3column and 1 observation that is filename starttime endtime

I want to create a single dataset from above mention file records.

I am unable to write macro

Kindly help me .

Thanks in advance
Respected Advisor
Posts: 3,799

Re: how to create loop inside macro

You don't need a macro you need a data step to read from a FILEREF of concatenated files.

[pre]
Example 1: Referencing External Files
You can reference external files from a concatenated list of files or directories.
The wildcard character * can be used in the FILENAME statement:

filename read ('c:\myfiles\a*.csv');
data new;
infile read;
input;
run;
[/pre]
Contributor
Posts: 45

Re: how to create loop inside macro

Posted in reply to data_null__
Dear Data_null_,
Thanks for your kind information.
I want to write a macro Please help me

Regards,
Arpit
Super User
Posts: 10,028

Re: how to create loop inside macro

Hi.
The last thing you need to do is to concatenate these datasets vertically.
I do not code it , you can do it by yourself. The following is the code i wrote before.
[pre]





[/pre]



%let subdir=D:\sasdata\a\b\;



filename dir pipe "dir

&subdir.*.csv /B"
;



data new;



 infile dir truncover end=last;



 input filename  $100.;



 filename=cats("&subdir",filename);



 call symputx(cats('path',_n_),filename);



 call symputx(cats('dsn',_n_),scan(scan(filename,5,'\'),1,'.'));



 if last then call symputx('nobs',_n_);



run;



%put _user_;



 



%macro import;



%do i=1 %to &nobs;



 



data WORK.&&dsn&i;                                      
;



      %let _EFIERR_ = 0; /* set the ERROR detection macro variable */



      infile "&&path&i" delimiter = ',' MISSOVER DSD lrecl=32767 firstobs=2 ;



        
informat VAR1
best32. ;



        
informat VAR2
$30. ;



        
informat VAR3
comma30. ;



        
informat VAR4
comma30.;



        
informat VAR5
comma30.;



        
informat VAR6
comma30.;



        
informat VAR7
comma30.;



        
informat VAR8
comma30.;



        

informat VAR9
best12.;



        
format VAR1
best12. ;



        

format VAR2
$30. ;



        
format VAR3
best32.2 ;



        
format VAR4
best32.2;



        
format VAR5
best32.2;



        

format VAR6
best32.2;



        
format VAR7
best32.2;



        
format VAR8
best32.2;



        
format VAR9
best12. ;



      input



                 
VAR1



                 
VAR2



                 
VAR3



                 
VAR4



                 
VAR5



                 
VAR6



                 
VAR7



                 
VAR8



                 
VAR9



      ;



 



                 
;



      if _ERROR_ then call symputx('_EFIERR_',1); 

/* set ERROR detection macro
variable */



      run;



%end;



%mend import;



 



%import




[pre]





[/pre]



Ksharp
Regular Contributor
Posts: 151

Re: how to create loop inside macro

Ask a Question
Discussion stats
  • 4 replies
  • 137 views
  • 0 likes
  • 4 in conversation