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

Hi all;

I have a question on the retain statement.

What I am attempting to do is essentially create a table of contents.

Depending on the count, it will dictate the number of pages so that if the count<=24 then one page, 24<=48 2 pages and what not.

What happens is that it seems to correctly calculate the page number for the first couple of lines and then it doesn't.

Any assistance would be greatly appreciated-Example of the output and the code follows below. It works for the first 4 entries.

Much obliged.

Lawrence

DATA

UNITS3;

SET

UNITS2;

by

NS1;

retain

PAGER PAGEX;

IF

0<count<=24 then PAGER=1

;

ELSE

IF 24<count<=48 then pager=2

;

ELSE

IF 48<count<=72 then PAGEr=3

;

IF

_N_=1 then pagex=0

;

PAGEX=pager+PAGEX;

PAGEURL=compress(

'page'

||PAGEX);

NS1COUNTPAGEXPAGEURLPAGER
10 CVT372page22
10ICC74page41
11 Neuro ICU95page51
11L / L14 Hem/Onc/BMT447page72
12 Long Spine/Ortho227page71
12 Moffitt - CCRC98page81

cc

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

Lawrence,

I can't test the following code at the moment, but I think that it probably does or comes close do doing what you want.  I don't use a retain statement because the form  pagex+ceil(count/24) automatically retains pagex.  I also didn't use the combination of if statements as I think the ceil function does what you want to do.  The main difference between the code, and your code, is that each record is output before the next applicable pagex is calculated.

DATA UNITS3;

  SET UNITS2;

  by NS1;

  IF _N_=1 then pagex=2;

  PAGEURL=compress('page'||PAGEX);

  output;

  pagex+ceil(count/24);

run;

View solution in original post

4 REPLIES 4
art297
Opal | Level 21

Some more info would be helpful, like what fields are actually in the input file, what ones are created here and what values do you expect to be assigned for them.  Some of the values shown in your example don't appear to match your description or code.

Plus, you include a by statement, but never refer to it and you retain pager but set it separately for each record.  Given your code, I would expect the second record to get a value for pagex of 3 .. not the 4 that is shown.

_LB
Fluorite | Level 6 _LB
Fluorite | Level 6

Art;

The data set essentially imports two variables, the NS (Nursing Station) and the count. This comes from a frequency output set;

The values created here are:

the pagex that is used for the table of contents,

the pageurl that is used later as links,

and the pager var that I am attempting to use as a way to correctly delegate the correct pagex variable depending on the number of records. 

the

Sorry for the wonky code in terms of display, this is a cut and paste issue.

In short, what I am attempting to do is to retain values of the pagex and pager so that in the case of

10CVT above, it should start on page 2, and since it has 37 records,

the next record 10ICC should start on page 4.

SInce that unit only has 7 records, 11 NEURO ICU should be on pg 6,

etc etc.

I hope this makes any sense Art as my head is now spinning as I am sure you can understand. and yes the values are not matching, and that is the problem. I am not sure if it is a logic issue or some other parameter that I am missing.

Thank you for your time.

Lawrence

art297
Opal | Level 21

Lawrence,

I can't test the following code at the moment, but I think that it probably does or comes close do doing what you want.  I don't use a retain statement because the form  pagex+ceil(count/24) automatically retains pagex.  I also didn't use the combination of if statements as I think the ceil function does what you want to do.  The main difference between the code, and your code, is that each record is output before the next applicable pagex is calculated.

DATA UNITS3;

  SET UNITS2;

  by NS1;

  IF _N_=1 then pagex=2;

  PAGEURL=compress('page'||PAGEX);

  output;

  pagex+ceil(count/24);

run;

_LB
Fluorite | Level 6 _LB
Fluorite | Level 6

Very elegant, Thank you!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 879 views
  • 0 likes
  • 2 in conversation