From: Joe Matise on
In 9.2 I think you can use the : operator to get everything starting with
'state_' with state:.

In your macro, I think you need to take the semicolon out of the macro loop=
,
at minimum, and put 'set' before the macro loop. I don't imagine your macr=
o
would work anyway, but you may be leaving something out. Not sure your
&,&&, etc. are properly balanced.

-Joe

On Mon, Dec 14, 2009 at 1:44 PM, SAS Swamy <sasswamy(a)gmail.com> wrote:

> Hello everyone,
> Can someone help me out with the following I am trying to dynamically
> combine few data sets and create one final data sets.
> Let say , If I have the following data sets =96 State wise
> State_il
> State_nj
> State_pa =85etc
> When I try with the normal code .
>
> data combined_st;
> set state_il state_ma state_nj state_pa state_NYC;
> run;
> this is working with no Issue ,
>
> If I try the same dynamically then I am able to assign only the last data
> set,
> &state_count =96 will be the count of end of the loop
> state_&&state_&loop - will be the values like state_il , state_nj =85etc
>
> %macro combined_states();
> data combined_states;
> %do loop =3D 1 %to &state_count;
> set state_&&state_&loop;
> %end;
> run;
> %mend combined_states;
>
> %combined_states;
>
> With this logic as you could see what ever the last data set only that
> data set is assigned to the final data set,
> how can I do a while loop on this ? So that I can assign all the data set=
s
> from the loop to one final data set.
> Please advice
>
> Thanks,
> - Swamy
>
From: saslearn chicago on
Thanks Joe,

I changed the semicolon outside the loop , it works fine now

- swamy
On Mon, Dec 14, 2009 at 2:54 PM, Joe Matise <snoopy369(a)gmail.com> wrote:

> In 9.2 I think you can use the : operator to get everything starting with
> 'state_' with state:.
>
> In your macro, I think you need to take the semicolon out of the macro
> loop, at minimum, and put 'set' before the macro loop. I don't imagine y=
our
> macro would work anyway, but you may be leaving something out. Not sure
> your &,&&, etc. are properly balanced.
>
> -Joe
>
> On Mon, Dec 14, 2009 at 1:44 PM, SAS Swamy <sasswamy(a)gmail.com> wrote:
>
>> Hello everyone,
>> Can someone help me out with the following I am trying to dynamically
>> combine few data sets and create one final data sets.
>> Let say , If I have the following data sets =96 State wise
>> State_il
>> State_nj
>> State_pa =85etc
>>
>> When I try with the normal code .
>>
>> data combined_st;
>> set state_il state_ma state_nj state_pa state_NYC;
>> run;
>> this is working with no Issue ,
>>
>> If I try the same dynamically then I am able to assign only the last dat=
a
>> set,
>> &state_count =96 will be the count of end of the loop
>> state_&&state_&loop - will be the values like state_il , state_nj =85et=
c
>>
>>
>> %macro combined_states();
>> data combined_states;
>> %do loop =3D 1 %to &state_count;
>> set state_&&state_&loop;
>> %end;
>> run;
>> %mend combined_states;
>>
>> %combined_states;
>>
>> With this logic as you could see what ever the last data set only that
>> data set is assigned to the final data set,
>> how can I do a while loop on this ? So that I can assign all the data se=
ts
>> from the loop to one final data set.
>> Please advice
>>
>> Thanks,
>> - Swamy
>>
>
>
From: Ian Whitlock on
Summary: Macro loop or list, and parameters
#iw-value=1

Swamy,

You may want

%macro combined_states();
data combined_states;
set
%do loop = 1 %to &state_count;
state_&&state_&loop /* NO ; */
%end;
; /* end of set statement */
run;
%mend combined_states;

%combined_states() /* NO ; */

Another possibility, just a little better in terms of
readability because it separates the SAS from the macro
is given by the following.

%macro combined_states();
%local state_list ;
%do loop = 1 %to &state_count;
%let state_list = &state_list state_&&state_&loop /* NO ; */
%end;

data combined_states;
set &state_list ;
run;
%mend combined_states;

%combined_states() /* NO ; */

You do not tell the source of the variables so let's make
up one to indicate some better design elements.

data states ;
input state $ @@ ;
cards ;
il nj pa nyc
;

%macro combined_states(cntl=);
%local state_list ;

proc sql noprint ;
select "state_" || state into :state_list separated by " "
from &cntl ;
quit ;

data combined_states;
set &state_list ;
run;
%mend combined_states;

%combined_states(cntl=states) /* NO ; */

Parameters help the reader understand the flow of control,
hence they make programs more readable. magic global variables
become a nightmare once one gets beyond baby programs.

Ian Whitlock
--------------

Date: Mon, 14 Dec 2009 14:44:39 -0500
From: SAS Swamy <sasswamy(a)GMAIL.COM>
Subject: Dynamic data set-Loop

Hello everyone,
Can someone help me out with the following I am trying to dynamically
combine few data sets and create one final data sets.
Let say , If I have the following data sets � State wise
State_il
State_nj
State_pa �etc
When I try with the normal code .

data combined_st;
set state_il state_ma state_nj state_pa state_NYC;
run;
this is working with no Issue ,

If I try the same dynamically then I am able to assign only the last
data
set,
&state_count � will be the count of end of the loop
state_&&state_&loop - will be the values like state_il , state_nj �etc

%macro combined_states();
data combined_states;
%do loop = 1 %to &state_count;
set state_&&state_&loop;
%end;
run;
%mend combined_states;

%combined_states;

With this logic as you could see what ever the last data set only that
data set is assigned to the final data set,
how can I do a while loop on this ? So that I can assign all the data
sets
from the loop to one final data set.
Please advice

Thanks,
- Swamy
From: =?ISO-8859-1?Q?Daniel_Fern=E1ndez?= on
After reading SAS-L experts to say to avoid writing SAS macros if possible,
I come this time with a non-macro solution (but it uses a macrovariable):

data states ;
input state $ @@ ;
cards ;
il nj pa nyc
;
run;

data _null_;
* for each row we pass the state name to the macrovariable 'state_loop';
do until (fin);
set states end=fin;
call symputx('state_loop','state_'||state);
*we append each dataset to the combined_states dataset, ONLY EXECUTE ONCE!;
call execute("proc append base=combined_states data=&state_loop force; run;");
end;
run;

Daniel Fernandez

2009/12/15 Ian Whitlock <iw1sas(a)gmail.com>:
> Summary: Macro loop or list, and parameters
> #iw-value=1
>
> Swamy,
>
> You may want
>
> %macro combined_states();
> data combined_states;
> set
> %do loop = 1 %to &state_count;
> state_&&state_&loop /* NO ; */
> %end;
> ; /* end of set statement */
> run;
> %mend combined_states;
>
> %combined_states() /* NO ; */
>
> Another possibility, just a little better in terms of
> readability because it separates the SAS from the macro
> is given by the following.
>
> %macro combined_states();
> %local state_list ;
> %do loop = 1 %to &state_count;
> %let state_list = &state_list state_&&state_&loop /* NO ; */
> %end;
>
> data combined_states;
> set &state_list ;
> run;
> %mend combined_states;
>
> %combined_states() /* NO ; */
>
> You do not tell the source of the variables so let's make
> up one to indicate some better design elements.
>
> data states ;
> input state $ @@ ;
> cards ;
> il nj pa nyc
> ;
>
> %macro combined_states(cntl=);
> %local state_list ;
>
> proc sql noprint ;
> select "state_" || state into :state_list separated by " "
> from &cntl ;
> quit ;
>
> data combined_states;
> set &state_list ;
> run;
> %mend combined_states;
>
> %combined_states(cntl=states) /* NO ; */
>
> Parameters help the reader understand the flow of control,
> hence they make programs more readable. magic global variables
> become a nightmare once one gets beyond baby programs.
>
> Ian Whitlock
> --------------
>
> Date: Mon, 14 Dec 2009 14:44:39 -0500
> From: SAS Swamy <sasswamy(a)GMAIL.COM>
> Subject: Dynamic data set-Loop
>
> Hello everyone,
> Can someone help me out with the following I am trying to dynamically
> combine few data sets and create one final data sets.
> Let say , If I have the following data sets � State wise
> State_il
> State_nj
> State_pa �etc
> When I try with the normal code .
>
> data combined_st;
> set state_il state_ma state_nj state_pa state_NYC;
> run;
> this is working with no Issue ,
>
> If I try the same dynamically then I am able to assign only the last
> data
> set,
> &state_count � will be the count of end of the loop
> state_&&state_&loop - will be the values like state_il , state_nj �etc
>
> %macro combined_states();
> data combined_states;
> %do loop = 1 %to &state_count;
> set state_&&state_&loop;
> %end;
> run;
> %mend combined_states;
>
> %combined_states;
>
> With this logic as you could see what ever the last data set only that
> data set is assigned to the final data set,
> how can I do a while loop on this ? So that I can assign all the data
> sets
> from the loop to one final data set.
> Please advice
>
> Thanks,
> - Swamy
>
From: Ian Whitlock on
Daniel,

I think you learned the wrong lesson. Sometimes it is worth avoiding
the
use of macros, but the desire to get a list of states into code does not
strike me as one of them. The technique is important.

Your double quotes are wrong and this is a macro error. Why introduce
the
macro variables if your point is to avoid macro. For a shorter and
simpler
one step non-macro try:

filename code temp ;
> data states ;
> input state $ @@ ;
put "proc append base=combined_states data=" state "force; run;" ;
>
> cards ;
> il nj pa nyc
> ;
%inc code ;

Perhaps PROC APPEND is better, but it is very questionable. PROC
APPEND will
only use the variables from IL. The code the OP was trying to execute
is
more flexible and efficient.

Incidentally, the line

>> %let state_list = &state_list state_&&state_&loop /* NO ; */

from my posting below is wrong. Of course it should be

%let state_list = &state_list state_&&state_&loop ;

Ian Whitlock
============
On Dec 15, 2009, at 4:58 AM, Daniel Fern�ndez wrote:

> After reading SAS-L experts to say to avoid writing SAS macros if
> possible,
> I come this time with a non-macro solution (but it uses a
> macrovariable):
>
> data states ;
> input state $ @@ ;
> cards ;
> il nj pa nyc
> ;
> run;
>
> data _null_;
> * for each row we pass the state name to the macrovariable
> 'state_loop';
> do until (fin);
> set states end=fin;
> call symputx('state_loop','state_'||state);
> *we append each dataset to the combined_states dataset, ONLY
> EXECUTE ONCE!;
> call execute("proc append base=combined_states data=&state_loop
> force; run;");
> end;
> run;
>
> Daniel Fernandez
>
> 2009/12/15 Ian Whitlock <iw1sas(a)gmail.com>:
>> Summary: Macro loop or list, and parameters
>> #iw-value=1
>>
>> Swamy,
>>
>> You may want
>>
>> %macro combined_states();
>> data combined_states;
>> set
>> %do loop = 1 %to &state_count;
>> state_&&state_&loop /* NO ; */
>> %end;
>> ; /* end of set statement */
>> run;
>> %mend combined_states;
>>
>> %combined_states() /* NO ; */
>>
>> Another possibility, just a little better in terms of
>> readability because it separates the SAS from the macro
>> is given by the following.
>>
>> %macro combined_states();
>> %local state_list ;
>> %do loop = 1 %to &state_count;
>> %let state_list = &state_list state_&&state_&loop /* NO ; */
>> %end;
>>
>> data combined_states;
>> set &state_list ;
>> run;
>> %mend combined_states;
>>
>> %combined_states() /* NO ; */
>>
>> You do not tell the source of the variables so let's make
>> up one to indicate some better design elements.
>>
>> data states ;
>> input state $ @@ ;
>> cards ;
>> il nj pa nyc
>> ;
>>
>> %macro combined_states(cntl=);
>> %local state_list ;
>>
>> proc sql noprint ;
>> select "state_" || state into :state_list separated by " "
>> from &cntl ;
>> quit ;
>>
>> data combined_states;
>> set &state_list ;
>> run;
>> %mend combined_states;
>>
>> %combined_states(cntl=states) /* NO ; */
>>
>> Parameters help the reader understand the flow of control,
>> hence they make programs more readable. magic global variables
>> become a nightmare once one gets beyond baby programs.
>>
>> Ian Whitlock
>> --------------
>>
>> Date: Mon, 14 Dec 2009 14:44:39 -0500
>> From: SAS Swamy <sasswamy(a)GMAIL.COM>
>> Subject: Dynamic data set-Loop
>>
>> Hello everyone,
>> Can someone help me out with the following I am trying to
>> dynamically
>> combine few data sets and create one final data sets.
>> Let say , If I have the following data sets � State wise
>> State_il
>> State_nj
>> State_pa �etc
>> When I try with the normal code .
>>
>> data combined_st;
>> set state_il state_ma state_nj state_pa state_NYC;
>> run;
>> this is working with no Issue ,
>>
>> If I try the same dynamically then I am able to assign only the last
>> data
>> set,
>> &state_count � will be the count of end of the loop
>> state_&&state_&loop - will be the values like state_il , state_nj �
>> etc
>>
>> %macro combined_states();
>> data combined_states;
>> %do loop = 1 %to &state_count;
>> set state_&&state_&loop;
>> %end;
>> run;
>> %mend combined_states;
>>
>> %combined_states;
>>
>> With this logic as you could see what ever the last data set only
>> that
>> data set is assigned to the final data set,
>> how can I do a while loop on this ? So that I can assign all the data
>> sets
>> from the loop to one final data set.
>> Please advice
>>
>> Thanks,
>> - Swamy
>>