BookmarkSubscribeRSS Feed
fl
Calcite | Level 5 fl
Calcite | Level 5
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?
6 REPLIES 6
Cynthia_sas
SAS Super FREQ
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
fl
Calcite | Level 5 fl
Calcite | Level 5
thanks a lot! 🙂
Ksharp
Super User
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;
Cynthia_sas
SAS Super FREQ
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
Ksharp
Super User
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
SPR
Quartz | Level 8 SPR
Quartz | Level 8
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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 1611 views
  • 0 likes
  • 4 in conversation