BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
oussema
Obsidian | Level 7

Hello All, I want to get an RTF output which contains no more than 4 observations per page and which goes to a next page when having a new ID.

Any suggestion ?

1 ACCEPTED SOLUTION

Accepted Solutions
oussema
Obsidian | Level 7

It works after doing a simple change:

data want;
  set have;
  retain pge cnt;
  by id;
  if _n_=1 then do;
    pge=1;
    cnt=1;
  end;
  else if first.id then do;
    pge=pge+1;
    cnt=1;
  end;
  else do;
    if cnt=4 then do;
      pge=pge+1;
      cnt=1;
    end;
    else cnt=cnt+1;
  end;
run;

thank you very much 🙂 

View solution in original post

4 REPLIES 4
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Yes, several ways of doing it.  First and most simple is to add a page into your data;

data want;
  set have;
  retain pge;
  if _n_=1 then pge=1;
  if mod(_n_,4)=0 then pge=pge+1;
run;

ods rtf file=...;
proc report data=want ...;
  by page;
...
run;
oussema
Obsidian | Level 7

Not resolved:When I have a new ID, some of their observation will not be in the appropriate page.

Example:

obs        ID          AE

1            1           nausea

2            1           headache

.             1           diarrhea

.             1           etc..

20          1           

21          2           

.             2           

.             2           

35          2

36          3

37          3 

38          3

39          4

.             4

.             4

In this example, we will have the first observation of ID number 3 with the last three observation of ID number 2 (all in page 9). But I want to have the last three observations of ID number 2 in page 9 and the three observations of ID 3 in page 10 and so on. 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

This is where specifying your problem fully really helps.  Post test data - in the form of a datastep, and exactly what you want to see out.  At a guess, you would need to change the logic in the datastep to what you want your output to look like, maybe something like:

data want;
  set have;
  retain pge cnt;
  by id;
  if _n_=1 then do;
    pge=1;
    cnt=1;
  end;
  else if first.id then do;
    pge=pge+1;
    cnt=cnt+1;
  end;
  else do;
    if cnt=4 then do;
      pge=pge+1;
      cnt=1;
    end;
    else cnt=cnt+1;
  end;
run;

ods rtf file=...;
proc report data=want ...;
  by page;
...
run;

This will count records within id and if > 4 then set new page, and if new id then set new page (its a bit overcomplicated, to show the workings).

oussema
Obsidian | Level 7

It works after doing a simple change:

data want;
  set have;
  retain pge cnt;
  by id;
  if _n_=1 then do;
    pge=1;
    cnt=1;
  end;
  else if first.id then do;
    pge=pge+1;
    cnt=1;
  end;
  else do;
    if cnt=4 then do;
      pge=pge+1;
      cnt=1;
    end;
    else cnt=cnt+1;
  end;
run;

thank you very much 🙂 

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

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
  • 4 replies
  • 1504 views
  • 0 likes
  • 2 in conversation