Help using Base SAS procedures

loops and proc univariate

Reply
New Contributor fl
New Contributor
Posts: 2

loops and proc univariate

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?
SAS Super FREQ
Posts: 8,743

Re: loops and proc univariate

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
New Contributor fl
New Contributor
Posts: 2

Re: loops and proc univariate

thanks a lot! Smiley Happy
Super User
Posts: 9,681

Re: loops and proc univariate

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;
SAS Super FREQ
Posts: 8,743

Re: loops and proc univariate

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
Super User
Posts: 9,681

Re: loops and proc univariate

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
Super Contributor
Super Contributor
Posts: 365

Re: loops and proc univariate

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
Ask a Question
Discussion stats
  • 6 replies
  • 303 views
  • 0 likes
  • 4 in conversation