DATA Step, Macro, Functions and more

Looping a data step through a string array

Reply
N/A
Posts: 1

Looping a data step through a string array

Apologies if this has been discussed before, I couldn't find a solution through a search.

I want to create a single data step framework that runs inside a macro loop driven by an array of string variables...

As simply as I can put it...

Say I have the array, days{7} sun mon tue wed thu fri sat;

I want to loop a single data step using this array to generate...

data sun; (each new dataset gets its name from the array variable)
set old;
keep var1 var2;
day = sun; (array variable can be used inside data step as well)
run;

data mon;
set old;
keep var1 var2;
day = mon;
run;

...and so on through the entire list of strings. Is this possible? I've been trying to use %DO_OVER but I can't seem to get the format right. Also SCAN has been another dry run. Is it just the case that the macro language does not support arrays, and I should just create separate data steps?

Thanks for any help or advice.
Frequent Contributor
Posts: 102

Re: Looping a data step through a string array

I am not clear what form your "array" is in. Is is inside a dataset, or do you have a delimited list, or somthing else?
Super User
Posts: 17,775

Re: Looping a data step through a string array

I think you need to look at processing data with a macro variable LIST rather than array.

See this paper:
http://www2.sas.com/proceedings/sugi30/028-30.pdf

HTH,
Reeza
Super User
Posts: 17,775

Re: Looping a data step through a string array

I think you need to look at processing data with a macro variable LIST rather than array.

See this paper:
http://www2.sas.com/proceedings/sugi30/028-30.pdf

HTH,
Reeza
Super User
Posts: 9,671

Re: Looping a data step through a string array

Hi.
It looks like you want to split dataset?



%macro split;



proc sql ;



 select distinct sex from sashelp.class;



 select distinct sex



   into : sex1 - : sex&sqlobs.



    from sashelp.class;



quit;



data %do i=1 %to &sqlobs.;



       &&sex&i



    
%end;



    
;



 set sashelp.class;



 select (sex);



  %do j=1 %to
&
sqlobs.;



   when("&&sex&j") output &&sex&j;



  %end;



  otherwise;



 end;



run;



%mend;



 



%split









[pre]


Ksharp
[/pre]

Message was edited by: Ksharp
Ask a Question
Discussion stats
  • 4 replies
  • 190 views
  • 0 likes
  • 4 in conversation