Hi,
I am a newbie to SAS and use SAS at work while I have experience in
using R for a while.
I would like to know how to automatically determine the length of
&vars (=5) instead of declaring 10 in the below script....
%let vars = A B C D E;
data _NULL_;
do i = 1 to 10; <---- this value of 10 is what I would like to
automate when I change &vars.
letter = scan(&vars., i, ' ');
put i letter;
if letter = " " then leave;
end;
run;
Any help is appreciated!!!
Thanks.
Taka
how can I automatically count the number of itesm in this macro
variable?
|
|
0
|
|
|
|
Reply
|
taquito2007 (6)
|
12/10/2009 7:31:35 PM |
|
On Dec 10, 2:31=A0pm, Takatsugu <taquito2...@gmail.com> wrote:
> Hi,
>
> I am a newbie to SAS and use SAS at work while I have experience in
> using R for a while.
>
> I would like to know how to automatically determine the length of
> &vars (=3D5) instead of declaring 10 in the below script....
>
> %let vars =3D A B C D E;
>
> data _NULL_;
> =A0 =A0do i =3D 1 to 10; =A0<---- this value of 10 is what I would like t=
o
> automate when I change &vars.
> =A0 =A0 =A0 letter =3D scan(&vars., i, ' ');
> =A0 =A0 =A0 put i letter;
> =A0 =A0 =A0 if letter =3D " " then leave;
> =A0 =A0end;
> run;
>
> Any help is appreciated!!!
>
> Thanks.
>
> Taka
>
> how can I automatically count the number of itesm in this macro
> variable?
http://support.sas.com/kb/26/152.html
|
|
0
|
|
|
|
Reply
|
Paige
|
12/10/2009 8:04:06 PM
|
|
see the Nitems macro:
http://www.sascommunity.org/mwiki/images/7/77/Nitems.sas
summary use functions compress and countc
Ron Fehd the macro maven CDC Atlanta GA USA RJF2 at cdc dot gov
> From: Takatsugu
> Sent: Thursday, December 10, 2009 2:32 PM
> Subject: how to count the number of elements in a macro variable?
>
> Hi,
>
> I am a newbie to SAS and use SAS at work while I have experience in
> using R for a while.
>
> I would like to know how to automatically determine the length of
> &vars (=5) instead of declaring 10 in the below script....
>
> %let vars = A B C D E;
>
> data _NULL_;
> do i = 1 to 10; <---- this value of 10 is what I would like to
> automate when I change &vars.
> letter = scan(&vars., i, ' ');
> put i letter;
> if letter = " " then leave;
> end;
> run;
>
> Any help is appreciated!!!
>
> Thanks.
>
> Taka
>
> how can I automatically count the number of itesm in this macro
> variable?
>
>
|
|
0
|
|
|
|
Reply
|
rjf2 (3354)
|
12/10/2009 8:28:16 PM
|
|
But you don't need to know the number of elements in VARS. And your
example demonstrates it. Just change the DO statement slightly...
%let vars = A B C D E;
data _NULL_;
do i = 1 by 1;
letter = scan("&vars.", i, ' ');
if missing(letter) then leave;
put i letter;
end;
run;
Your example further implies that you are somehow thinking about some
processing for the indiviual variables in &VARS. If that is the case
then I would suggest a different method to "expand" the list into
indivitual parts. This method can also process "SAS Variables Lists".
Like A1-A10, _CHARACTER_ etc. If you are new to SAS you will be
happy to learn how using "SAS Variables Lists" can save you typing and
in many cases make your programs dynamic.
* Create example data;
data test;
retain &vars 1;
attrib &vars label='Example';
run;
%let vars = a--e;
proc transpose data=test(obs=0) out=varlist;
var &vars;
run;
proc sql noprint;
select _name_ into :vars separated by ' ' from varlist;
%let vars0 = &sqlobs;
quit;
run;
%put NOTE: VARS(&vars0)=&vars;
This data set is far more useful than the result of SCANing the words in &VARS.
On 12/10/09, Takatsugu <taquito2007@gmail.com> wrote:
> Hi,
>
> I am a newbie to SAS and use SAS at work while I have experience in
> using R for a while.
>
> I would like to know how to automatically determine the length of
> &vars (=5) instead of declaring 10 in the below script....
>
> %let vars = A B C D E;
>
> data _NULL_;
> do i = 1 to 10; <---- this value of 10 is what I would like to
> automate when I change &vars.
> letter = scan(&vars., i, ' ');
> put i letter;
> if letter = " " then leave;
> end;
> run;
>
> Any help is appreciated!!!
>
> Thanks.
>
> Taka
>
> how can I automatically count the number of itesm in this macro
> variable?
>
|
|
0
|
|
|
|
Reply
|
iebupdte
|
12/10/2009 9:00:34 PM
|
|
> -----Original Message-----
> From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of
> Takatsugu
> Sent: Thursday, December 10, 2009 11:32 AM
> To: SAS-L@LISTSERV.UGA.EDU
> Subject: how to count the number of elements in a macro variable?
>
> Hi,
>
> I am a newbie to SAS and use SAS at work while I have experience in
> using R for a while.
>
> I would like to know how to automatically determine the length of
> &vars (=5) instead of declaring 10 in the below script....
>
> %let vars = A B C D E;
>
> data _NULL_;
> do i = 1 to 10; <---- this value of 10 is what I would like to
> automate when I change &vars.
> letter = scan(&vars., i, ' ');
> put i letter;
> if letter = " " then leave;
> end;
> run;
>
> Any help is appreciated!!!
>
> Thanks.
>
> Taka
>
> how can I automatically count the number of itesm in this macro
> variable?
1. To answer your original question, it is pretty simple.
data _NULL_;
do i = 1 to countw("&vars");
letter = scan("&vars", i, ' ');
put i letter;
end;
run;
However, Data _null_ has given you some good advice and some practical examples of other approaches using lists in SAS.
2. If as the macro name suggests, you actually have some variables that you want to do some processing on, the macro variable may actually be used as a list. For example , if the variables are numeric and you want to sum them up,
Sum_vars = sum(of &vars);
Or if you want to process them using array processing, you do something like,
Array v[*] &vars;
do i= 1 to dim(v);
if v[i] = . then do; /*something*/ end;
end;
That being said, sometimes the best (most useful) help you get is when you describe what data you have, what you want, and what you have tried so far, and often an expert comes along and shows you a better way to accomplish your goals.
Hope this is helpful,
Dan
Daniel J. Nordlund
Washington State Department of Social and Health Services
Planning, Performance, and Accountability
Research and Data Analysis Division
Olympia, WA 98504-5204
|
|
0
|
|
|
|
Reply
|
NordlDJ
|
12/10/2009 11:00:38 PM
|
|
|
4 Replies
303 Views
(page loaded in 0.079 seconds)
|