DATA Step, Macro, Functions and more

calculation for every value (SUBJECT)

Reply
Contributor
Posts: 38

calculation for every value (SUBJECT)

Hi, I need some do loop part for repeating calculation for every values .

PROC Star
Posts: 1,400

Re: calculation for every value (SUBJECT)

You have to be more specific. Show us your problem, preferably with some sample data.

 

Sound like By Group Processing is your answer though. 

Super User
Super User
Posts: 9,829

Re: calculation for every value (SUBJECT)

No you don't, a datastep is a loop over the observations, hence any code in a datastep is inherently used in a loop. 

To post a question to get acurate answers do the following:

1) Post test data in the form of a datastep using the {i} code window

2) Post example output required

3) Explain the process  

Contributor
Posts: 38

Re: calculation for every value (SUBJECT)


proc sql noprint ;
select count(distinct subjid) into: nobs from final ;
quit ;
%let nobs=&nobs ;
%put &nobs ;

%if &nobs ne 0 %then %do ;
data _null_;
set final ;
call symput("subjid"||strip(put(_n_,best.)), strip(subjid)) ;
run;

%do i=1 %to &nobs ;
options nobyline;

.....

.....

....

proc sgplot data=final(where=(subjid="&&subjid&i."))

..

..

..quit;
%end ;
%end ; 

I Solved like this and thats working, thanks anyway .

Super User
Super User
Posts: 9,829

Re: calculation for every value (SUBJECT)

Well, that at least is a small bit of information that would have helped us.  There are two methods for doing this which will result in smaller more robust code.  The first uses only base sas which is always 100% better than any code involving macro:

data final;
  merge final sashelp.vtable (where=(libname="WORK" and memname="FINAL"));
  subjid=cats(put(nobs,best.),subjid);
run;

proc sgplot data=final;
  by subjid;

...
run;

This uses by group processing - a fundamental basic technique for working with by groups so you don't have to rewrite it.

The second uses some code generation:

data final;
  merge final sashelp.vtable (where=(libname="WORK" and memname="FINAL"));
  subjid=cats(put(nobs,best.),subjid);
run;

data _null_;
  set final;
  call execute (cats('proc sgplot data=final (where=(subjid=',subjid,'));...;run;'));
run;

This method you do not need loops or counters, the dataset itself will drive the number of iterations.  I would only go this way however if the by grouping does not do everything you need (those very rare circumstances) as by groups are more robust and simplfy the code a lot.

Contributor
Posts: 38

Re: calculation for every value (SUBJECT)

Thanks
Super User
Super User
Posts: 9,829

Re: calculation for every value (SUBJECT)

There is actually a third way, second only to by grouping.  And this using dynamic variables in proc template.  Sgplot creates a template behind the scenes - you can see this using tmpltout option in sgplot.  Modify the template code somewhat:

proc template;
...
  dynamic subj;
...
run;

proc sgrender...;
  dynamic subj=...;
run;
Contributor
Posts: 38

Re: calculation for every value (SUBJECT)

i have used gplot, and during this i have new problem, like every graph should have name part from SUBJD variable,
Super User
Super User
Posts: 9,829

Re: calculation for every value (SUBJECT)

There is no need to use gplot, it is very old.  Use sgplot.  Please give examples of what you mean, test data, code, required output etc.  With a by group in sgplot it is simple:

proc sgplot...;
  by subjid;
  title "The Subject: #byval1";
  ...;
run;
Contributor
Posts: 38

Re: calculation for every value (SUBJECT)

Capture.PNG

Super User
Super User
Posts: 9,829

Re: calculation for every value (SUBJECT)

?

Contributor
Posts: 38

Re: calculation for every value (SUBJECT)

should be patient 001

patient 002

Super User
Super User
Posts: 9,829

Re: calculation for every value (SUBJECT)

Please review what I posted, particularly:

  title "The Subject: #byval1";

This is not #subjid or anything else, but # byval1, indiccating the Value of the First by group variable. 

Ask a Question
Discussion stats
  • 12 replies
  • 237 views
  • 0 likes
  • 3 in conversation