DATA Step, Macro, Functions and more

retain statement

Accepted Solution Solved
Reply
Regular Contributor
Regular Contributor
Posts: 170
Accepted Solution

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

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

cc


Accepted Solutions
Solution
‎08-29-2011 04:54 PM
PROC Star
Posts: 7,471

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;

View solution in original post


All Replies
PROC Star
Posts: 7,471

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.

Regular Contributor
Regular Contributor
Posts: 170

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

Solution
‎08-29-2011 04:54 PM
PROC Star
Posts: 7,471

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;

Regular Contributor
Regular Contributor
Posts: 170

retain statement

Very elegant, Thank you!

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

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