BookmarkSubscribeRSS Feed
Vahe_Mar
Obsidian | Level 7

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

Thanks,
Vahe
12 REPLIES 12
PeterClemmensen
Tourmaline | Level 20

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

 

Sound like By Group Processing is your answer though. 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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  

Vahe_Mar
Obsidian | Level 7


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 .

Thanks,
Vahe
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

Vahe_Mar
Obsidian | Level 7
Thanks
Thanks,
Vahe
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;
Vahe_Mar
Obsidian | Level 7
i have used gplot, and during this i have new problem, like every graph should have name part from SUBJD variable,
Thanks,
Vahe
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;
Vahe_Mar
Obsidian | Level 7

Capture.PNG

Thanks,
Vahe
RW9
Diamond | Level 26 RW9
Diamond | Level 26

?

Vahe_Mar
Obsidian | Level 7

should be patient 001

patient 002

Thanks,
Vahe
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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. 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 12 replies
  • 1592 views
  • 0 likes
  • 3 in conversation