Hello,
Consider the following program;
data have;
input a;
cards;
1
2
;
run;
proc sort data=sashelp.class out=class;
by sex;
run;
data test;
set have;
do i=1 to nobs;
set class nobs=nobs;
by sex;
if first.sex then total_weight=0;
total_weight+weight;
output;
end;
run;
When execution reaches observation 2 of the have dataset, the whole sashelp.class has already been entirely read.
Thus the consecutive "set sashelp.class" will stop the data step.
I can not use the point= option because of by group processing.
Thanks for any advice to overcome this difficulty.
For the more complex problem, I confess to not reading it through sufficiently. But it should still be possible to use POINT= to read every observation without a BY statement. You would need to set up multiple buckets for the calculations (analogous to one set of calculations for F and one for M) when looping through with POINT=.
Sorry for not being clear enough. Here is what the resulting dataset should look like :
a | i | Name | Sex | Age | Height | Weight | total_weight |
1 | 1 | Alice | F | 13 | 56.5 | 84.0 | 84.0 |
1 | 2 | Barbara | F | 13 | 65.3 | 98.0 | 182.0 |
1 | 3 | Carol | F | 14 | 62.8 | 102.5 | 284.5 |
1 | 4 | Jane | F | 12 | 59.8 | 84.5 | 369.0 |
1 | 5 | Janet | F | 15 | 62.5 | 112.5 | 481.5 |
1 | 6 | Joyce | F | 11 | 51.3 | 50.5 | 532.0 |
1 | 7 | Judy | F | 14 | 64.3 | 90.0 | 622.0 |
1 | 8 | Louise | F | 12 | 56.3 | 77.0 | 699.0 |
1 | 9 | Mary | F | 15 | 66.5 | 112.0 | 811.0 |
1 | 10 | Alfred | M | 14 | 69.0 | 112.5 | 112.5 |
1 | 11 | Henry | M | 14 | 63.5 | 102.5 | 215.0 |
1 | 12 | James | M | 12 | 57.3 | 83.0 | 298.0 |
1 | 13 | Jeffrey | M | 13 | 62.5 | 84.0 | 382.0 |
1 | 14 | John | M | 12 | 59.0 | 99.5 | 481.5 |
1 | 15 | Philip | M | 16 | 72.0 | 150.0 | 631.5 |
1 | 16 | Robert | M | 12 | 64.8 | 128.0 | 759.5 |
1 | 17 | Ronald | M | 15 | 67.0 | 133.0 | 892.5 |
1 | 18 | Thomas | M | 11 | 57.5 | 85.0 | 977.5 |
1 | 19 | William | M | 15 | 66.5 | 112.0 | 1089.5 |
2 | 1 | Alice | F | 13 | 56.5 | 84.0 | 84.0 |
2 | 2 | Barbara | F | 13 | 65.3 | 98.0 | 182.0 |
2 | 3 | Carol | F | 14 | 62.8 | 102.5 | 284.5 |
2 | 4 | Jane | F | 12 | 59.8 | 84.5 | 369.0 |
2 | 5 | Janet | F | 15 | 62.5 | 112.5 | 481.5 |
2 | 6 | Joyce | F | 11 | 51.3 | 50.5 | 532.0 |
2 | 7 | Judy | F | 14 | 64.3 | 90.0 | 622.0 |
2 | 8 | Louise | F | 12 | 56.3 | 77.0 | 699.0 |
2 | 9 | Mary | F | 15 | 66.5 | 112.0 | 811.0 |
2 | 10 | Alfred | M | 14 | 69.0 | 112.5 | 112.5 |
2 | 11 | Henry | M | 14 | 63.5 | 102.5 | 215.0 |
2 | 12 | James | M | 12 | 57.3 | 83.0 | 298.0 |
2 | 13 | Jeffrey | M | 13 | 62.5 | 84.0 | 382.0 |
2 | 14 | John | M | 12 | 59.0 | 99.5 | 481.5 |
2 | 15 | Philip | M | 16 | 72.0 | 150.0 | 631.5 |
2 | 16 | Robert | M | 12 | 64.8 | 128.0 | 759.5 |
2 | 17 | Ronald | M | 15 | 67.0 | 133.0 | 892.5 |
2 | 18 | Thomas | M | 11 | 57.5 | 85.0 | 977.5 |
2 | 19 | William | M | 15 | 66.5 | 112.0 | 1089.5 |
proc sort data=sashelp.class out=class;
by sex;
run;
data class;
i = _n_;
set class nobs=nobs;
by sex;
if first.sex then total_weight=0;
total_weight+weight;
run;
proc surveyselect noprint rep=2 rate=1 out=stacked(rename=(replicate=a));
run;
Thanks @data_null__ and @Astounding for your answers. I tried to simplify the problem in order to avoid distracting readers with unnecessary details but i realize that it can give a misleading view of what i am trying to do.
The computation of sums only served as an example to justify a by statement. Actually, what is done in
the loop is dependant on the data in the currently processed row of the have dataset. That is,
for each row in have, i want to read the whole other dataset and create new variables that depend
on both datasets columns.
I just don't figure out how to restart reading the second dataset from observation 1 for each
new observation of the have dataset.
If you want the full context, i was trying to generalize my last answer in the following thread :
https://communities.sas.com/t5/SAS-Programming/Pairwise-comparisons-in-loops/m-p/497468#M131862
in order to take different sectors/quarters into account and stumbled upon this difficulty.
It should be easy enough to summarize SASHELP.CLASS down to 2 observations (M and F) before bringing in HAVE. Then you could use POINT= (or possibly a sort and merge if that is a better choice).
For the more complex problem, I confess to not reading it through sufficiently. But it should still be possible to use POINT= to read every observation without a BY statement. You would need to set up multiple buckets for the calculations (analogous to one set of calculations for F and one for M) when looping through with POINT=.
Thanks, i finally managed to overcome the difficulty. I needed in particular first.var and last.var created by the by statement so i used a prior data step to save them as new columns of the dataset. The by statement thus became useless and i could use the POINT= option.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.