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

## retain statement

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);

 NS1 COUNT PAGEX PAGEURL PAGER 10 CVT 37 2 page2 2 10ICC 7 4 page4 1 11 Neuro ICU 9 5 page5 1 11L / L14 Hem/Onc/BMT 44 7 page7 2 12 Long Spine/Ortho 22 7 page7 1 12 Moffitt - CCRC 9 8 page8 1

cc

1 ACCEPTED SOLUTION

Accepted Solutions
Opal | Level 21

## retain statement

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;

4 REPLIES 4
Opal | Level 21

## retain statement

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.

Fluorite | Level 6

## retain statement

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

Opal | Level 21

## retain statement

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;

Fluorite | Level 6

## retain statement

Very elegant, Thank you!

Discussion stats
• 4 replies
• 968 views
• 0 likes
• 2 in conversation