Lekshmi Vijayakumar
Lekshmi Vijayakumar

Reputation: 15

How to create a string with values of previous row SAS

I have a table with list of account numbers bucket n monthperiod. I need to make a bucket string like below..please help (base SAS)

ACC Bucket Month bucketstring
123      0        jan18     0
123       1        feb18   10
123        2       mar18  210
345       0        feb18    0
345       1        mar18  10

Upvotes: 0

Views: 131

Answers (1)

Richard
Richard

Reputation: 27516

The retain statement is used to maintain the value of a non-set variable over the iterations of the implicit loop that happens during the DATA step.

This example will work with ACC groups having upto 15 months (0..15). ACCs with more months will see a message put in the log.

data want;
  set have;
  by ACC;

  length bucketstring $20; * bucketstring might have to be made longer;
  retain buckstring;

  if length (bucketstring) = 20 and not first.ACC then
    put 'ERROR: bucketstring has to be longer for the case of ' ACC= month=;

  if first.ACC
    then bucketstring = cats(month);
    else bucketstring = cats(bucketstring,month);
run;

The cats function concatenates items. The items are automatically stripped of leading and trailing spaces, as well as automatically converting a number-item to a character value if necessary.

Upvotes: 1

Related Questions