Running the same code for different data sets

Reply
Contributor
Posts: 57

Running the same code for different data sets

[ Edited ]

Hi all,

 

I just want to ask a quick question. I have 500 datasets and I want to run the same code for all datasets. The name of my datasets are TV1, TV2, .... TV500. I think I need to use MACRO statements here. So, firstly I write the code for just one dataset:

 

 

     data KHA. TV1A;
     set KHA. TV1;
     by datetime;
     if first.datetime then V = 0;
     V+volume;
     if last.datetime then output;
     run;

 

Then I test the following code (I got it from your communities, as well), however I cannot get results: 

 

%MACRO repeat(dslist=);
%DO i=1 %TO %SYSFUNC(COUNTW(&dslist));
     %LET ds = %SCAN(&dslist, &i);
 
     DATA &ds;
          SET &ds;
 
     by datetime; 
     if first.datetime then V = 0; 
     V+volume; 
     if last.datetime then output;
          END;
     RUN;
%END;
%MEND repeat;
%repeat(dslist= TV1, TV2, .... TV500);

 

 

 

Could you please help me about that? Could you please give me any other ways to run the same code for multiple assets?

 

 

Super User
Posts: 22,480

Re: Running the same code for different data sets

Posted in reply to Khaladdin

Try passing the list in without comma's separating the data set names. 

Super User
Posts: 22,480

Re: Running the same code for different data sets

Also, this is why you shouldn't split your data sets in the first place. Your code doesn't show any mistakes from a quick first glance. 

I would suggest running it with a list of two data sets and then posting the output. 

Make sure you have the macro debuggging options set:

 

options mprint symbolgen mlogic;
Respected Advisor
Posts: 2,500

Re: Running the same code for different data sets

Posted in reply to Khaladdin

Khaladdin wrote:

Hi all,

 

I just want to ask a quick question. I have 500 datasets and I want to run the same code for all datasets. The name of my datasets are TV1, TV2, .... TV500. I think I need to use MACRO statements here. So, firstly I write the code for just one dataset:

 


@Reeza hints at a better answer, one that doesn't use macros. Put all the data into a single data set, and then use BY group processing to perform the analysis. Simple, efficient, requires less programming knowledge, it's a win-win-win.

--
Paige Miller
Ask a Question
Discussion stats
  • 3 replies
  • 120 views
  • 0 likes
  • 3 in conversation