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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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