SAS Macro to import multiple text files to individual datasets

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

SAS Macro to import multiple text files to individual datasets

[ Edited ]

Hello,

 

I am relatively new to sas, esspecially macros. I hope you could help me.

 

Basically, is there a way to create a loop or tell sas to refer to unique text variables in order to create different datasets? Here is the code, but as of now if I had to create two datasets I would need to have another data step. 

 

%let path=blablabla; 
%let c1=text1;
%let c2=text2;
%let f1=departures;

data test_&c1;
infile "&path.&f1._&c1..csv";
input Date Origin Weight;
run;

 

Thank you for your response.


Accepted Solutions
Solution
‎03-26-2016 04:38 AM
Super User
Posts: 19,155

Re: SAS Macros

 

You could read all files into one dataset and then split the file. 

https://communities.sas.com/t5/SAS-Communities-Library/How-do-I-write-a-macro-to-import-multiple-tex...

 

https://communities.sas.com/t5/SAS-Communities-Library/How-do-I-write-a-macro-to-split-my-data-set-i...

 

%macro import_data(param1, param2);

*data step using macro variables param1, param2;

%mend;

Then create a data step that has all of the parameter files you need to import and use Call Execute to execute the macro multiple times. 

There are many samples of this code in the forum. 

View solution in original post


All Replies
Super Contributor
Posts: 414

Re: SAS Macros

[ Edited ]

You can add all sorts of looping and logic if you write a macro definition. This is macro coding beyond the basic variable substitution you present in your example.

 

Repeating SAS code in a loop would be something along the lines of (untested):

 

%macro repeat;

%let numds=2;

%let ds1=test1;

%let ds2=test2;

%let c1=text1;

%let c2=text2;

%do i=1 %to &numds;

data &&test&i;

   text="&&c&i";

run;

%end;

%mend;

%repeat

 

This is just one (albeit common) scenario of macro use. There are many many more. Consider your macro adventures to have begun. I hope you will appreciate the power of it. I know I do.

 

Hope this helps,

- Jan

New Contributor
Posts: 2

Re: SAS Macros

Sounds interesting. Although I implied a bit different outcome.

 

Suppose I need to create two datasets: test_text1 and test_text2 from two corresponding files located in d:/blabla/departures_text1 an d:/blabla/departures_text2. I could create two separate datasteps, but in case there are more than two datasets, then I am in trouble.

 

Any ideas? 

Super Contributor
Posts: 414

Re: SAS Macros

I was assuming my example would easily be extrapolated from 2 to n datasets. And the source of the dataset names can be anything, not just %let statements. Eg. use SQL to create it:

 

proc sql noprint;

select distinct ds from xyz into :ds1-:ds999;

quit;

%let ntables=&sqlobs;

 

.... etc ...

 

I suggest you look up the SAS guide to Macro processing for loads of knowledge and examples. A great start is also http://www2.sas.com/proceedings/sugi29/243-29.pdf. And Art Carpenter's Complete Guide to the SAS Macro Language.

 

Hope this helps,

- Jan.

Solution
‎03-26-2016 04:38 AM
Super User
Posts: 19,155

Re: SAS Macros

 

You could read all files into one dataset and then split the file. 

https://communities.sas.com/t5/SAS-Communities-Library/How-do-I-write-a-macro-to-import-multiple-tex...

 

https://communities.sas.com/t5/SAS-Communities-Library/How-do-I-write-a-macro-to-split-my-data-set-i...

 

%macro import_data(param1, param2);

*data step using macro variables param1, param2;

%mend;

Then create a data step that has all of the parameter files you need to import and use Call Execute to execute the macro multiple times. 

There are many samples of this code in the forum. 

Super User
Posts: 19,155

Re: SAS Macros

If you want a dataset for each file then you wil need either a macro or call execute, or a combination. 

 

Otherwise, there are several ways to input all files into one dataset.

Super User
Super User
Posts: 7,720

Re: SAS Macro to import multiple text files to individual datasets

It also depends on the data you are importing.  Is it the same structure for each CSV file?  If it is, why the need to split them into different datasets?  You could do something like the below, if you really have to, however I would say if your new to SAS you will want to do some research on Base SAS - which is the actual programming language - before you start messing around with Marco or call execute.  Its a bit like taking a JCV out to learn driving in.

data _null_;
  do i="file1","file2"...;
    call execute(cat('data ',i,'; infile "c:\',strip(i),'.csv"; input date weight origin; run;'));
  end;
run;
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 533 views
  • 3 likes
  • 4 in conversation