- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Posted 05-17-2011 05:56 PM
(2266 views)
i want to run a proc univariate several times for different variables. can i do it inside a loop?
intuitively, this is what i would expect to do
DO sv = 'sv1','sv2','sv3';
proc univariate data=sample0 noprint;
where exchcd=1;
var sv;
by date;
output out=concat('nyse_',sv) pctlpts=30 70 pctlpre=sv;
run;
end;
what is wrong here?
intuitively, this is what i would expect to do
DO sv = 'sv1','sv2','sv3';
proc univariate data=sample0 noprint;
where exchcd=1;
var sv;
by date;
output out=concat('nyse_',sv) pctlpts=30 70 pctlpre=sv;
run;
end;
what is wrong here?
6 REPLIES 6
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi:
You are on the right track, but with the wrong type of DO loop. The regular DO statement belongs to a DATA step program. Since you cannot use PROC UNIVARIATE "inside" a DATA step program, you need to look to other techniques.
The SAS language has a separate syntax to replicate and generate "code blocks" for compilation and execution. That separate syntax belongs to the SAS Macro facility.
This paper is a good introduction to the SAS Macro Facility.
http://www2.sas.com/proceedings/sugi28/056-28.pdf
Previous forum postings have covered topics such as this. There are several different ways to accomplish what you want to do....from the very simple to the very complex. I'd recommend reading the above paper and a few of these and then see what you can accomplish -- starting with some of the simple techniques.
http://www.ats.ucla.edu/stat/sas/code/macro_repeatedproc.htm
http://www2.sas.com/proceedings/sugi29/243-29.pdf
http://www.nesug.org/proceedings/nesug03/bt/bt009.pdf
http://scott.sherrillmix.com/blog/programmer/sas-macros-letting-sas-do-the-typing/
cynthia
You are on the right track, but with the wrong type of DO loop. The regular DO statement belongs to a DATA step program. Since you cannot use PROC UNIVARIATE "inside" a DATA step program, you need to look to other techniques.
The SAS language has a separate syntax to replicate and generate "code blocks" for compilation and execution. That separate syntax belongs to the SAS Macro facility.
This paper is a good introduction to the SAS Macro Facility.
http://www2.sas.com/proceedings/sugi28/056-28.pdf
Previous forum postings have covered topics such as this. There are several different ways to accomplish what you want to do....from the very simple to the very complex. I'd recommend reading the above paper and a few of these and then see what you can accomplish -- starting with some of the simple techniques.
http://www.ats.ucla.edu/stat/sas/code/macro_repeatedproc.htm
http://www2.sas.com/proceedings/sugi29/243-29.pdf
http://www.nesug.org/proceedings/nesug03/bt/bt009.pdf
http://scott.sherrillmix.com/blog/programmer/sas-macros-letting-sas-do-the-typing/
cynthia
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
thanks a lot! 🙂
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Maybe :
[pre]
%DO sv = sv1 sv2 sv3 ;
proc univariate data=sample0 noprint;
where exchcd=1;
var &sv;
by date;
output out=nyse_&sv pctlpts=30 70 pctlpre=sv;
run;
%end;
[pre]
%DO sv = sv1 sv2 sv3 ;
proc univariate data=sample0 noprint;
where exchcd=1;
var &sv;
by date;
output out=nyse_&sv pctlpts=30 70 pctlpre=sv;
run;
%end;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi:
It's very useful to run code before posting it for usage by folks who are new to a particular concept, like macro processing. For example, your posted code generates an ERROR message:
[pre]
463 %DO sv = sv1 sv2 sv3 ;
ERROR: Expected %TO not found in %DO statement. A dummy macro will be compiled.
[/pre]
And, your code snippet implies that %DO can be placed in "open" code, which is not possible. A %DO statement must be used inside a macro program.
cynthia
It's very useful to run code before posting it for usage by folks who are new to a particular concept, like macro processing. For example, your posted code generates an ERROR message:
[pre]
463 %DO sv = sv1 sv2 sv3 ;
ERROR: Expected %TO not found in %DO statement. A dummy macro will be compiled.
[/pre]
And, your code snippet implies that %DO can be placed in "open" code, which is not possible. A %DO statement must be used inside a macro program.
cynthia
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi.
Cynthia. The code i wrote is not tested, just show an example.
Yes.You maybe right for new folk it is very helpful to find some information about this
by himself before posting .
And I also sure macro can do it.
Sorry.I think use %scan and %do %while() and %eval() to get that.
Ksharp Message was edited by: Ksharp
Cynthia. The code i wrote is not tested, just show an example.
Yes.You maybe right for new folk it is very helpful to find some information about this
by himself before posting .
And I also sure macro can do it.
Sorry.I think use %scan and %do %while() and %eval() to get that.
Ksharp Message was edited by: Ksharp
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hello FL,
Do you really need a do loop? You could get all necessary information by the following code:
[pre]
proc univariate data=sample0 noprint;
where exchcd=1;
var sv1 sv2 sv3;
by date;
output out=nyse pctlpts=30 70 pctlpre=sv1 sv2 sv3;
run;
[/pre]
Sincerely,
SPR
Do you really need a do loop? You could get all necessary information by the following code:
[pre]
proc univariate data=sample0 noprint;
where exchcd=1;
var sv1 sv2 sv3;
by date;
output out=nyse pctlpts=30 70 pctlpre=sv1 sv2 sv3;
run;
[/pre]
Sincerely,
SPR