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

Hi everyone, I'm working with informations about customers along 136 months.

What I'm working on is an specific information about those customers that express a situation that can from a month to another. With this data in hands I'm building a report to specify the duration (in months) of each situation.

 

For example, concatenating all these situation for an hypothetic customer we would have:

 

customer_historic='00000666668888888888880000000000000000777777777777777777770000000006666666'

 

At first I could say that on the first five months the situation was 0 and lasted for 5 months.

And since the 6th month the situation began to be 6, and it lasted for 5 months as well.

And then, from the 11th month it started to be 8, and it lasted for 8 months

.....

 

 

But, using formulas, I just can identify the beginning of this sequence, which could be done using:

 

FIRST_NUMB_SEQUENCE = INDEX(customer_historic,'0');

Which the result would be FIRST_NUMB_SEQUENCE = '1'.

 

Does anyone know a way to identify when, along the sequence, the digit starts to be different from 6 ? Is possible to do that using INDEX function ?

 

Or an alternative way.

 

Thanks in advance.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

Here one way of doing this. Code based on example from docu here.

data have;
  id=1;
  customer_historic='00000666668888888888880000000000000000777777777777777777770000000006666666';
  output;
  id=2;
  call missing(customer_historic);
  output;
  stop;
run;

data want(drop=_:);
  set have;
  length value $1;
  _exprid = prxparse('/(\d)\1*/');
  _start = 1;
  _stop = lengthn(customer_historic);

  if _stop=0 then output;
  else
    do;
    /* Use PRXNEXT to find the first instance of the pattern, */
    /* then use DO WHILE to find all further instances.       */
    /* PRXNEXT changes the _start parameter so that searching  */
    /* begins again after the last match.                     */
    call prxnext(_exprid, _start, _stop, customer_historic, first_numb_sequence , _len);

    do while (first_numb_sequence  > 0);
      value = substrn(customer_historic, first_numb_sequence , 1);
      last_numb_sequence=first_numb_sequence+_len-1;
      output;
      call prxnext(_exprid, _start, _stop, customer_historic, first_numb_sequence , _len);
    end;
  end;
run;

proc print;
run;

 

Capture.JPG 

View solution in original post

3 REPLIES 3
mkeintz
PROC Star

Many suggestions might be made, but why not show us what the resulting data set would look like?  That will help narrow down the relevant answers.  For instance, if a customer's history has, say 7 "regimes", do you want seven output records (and what would they look like)?   If some other structure, what would it look like?

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Patrick
Opal | Level 21

Here one way of doing this. Code based on example from docu here.

data have;
  id=1;
  customer_historic='00000666668888888888880000000000000000777777777777777777770000000006666666';
  output;
  id=2;
  call missing(customer_historic);
  output;
  stop;
run;

data want(drop=_:);
  set have;
  length value $1;
  _exprid = prxparse('/(\d)\1*/');
  _start = 1;
  _stop = lengthn(customer_historic);

  if _stop=0 then output;
  else
    do;
    /* Use PRXNEXT to find the first instance of the pattern, */
    /* then use DO WHILE to find all further instances.       */
    /* PRXNEXT changes the _start parameter so that searching  */
    /* begins again after the last match.                     */
    call prxnext(_exprid, _start, _stop, customer_historic, first_numb_sequence , _len);

    do while (first_numb_sequence  > 0);
      value = substrn(customer_historic, first_numb_sequence , 1);
      last_numb_sequence=first_numb_sequence+_len-1;
      output;
      call prxnext(_exprid, _start, _stop, customer_historic, first_numb_sequence , _len);
    end;
  end;
run;

proc print;
run;

 

Capture.JPG 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 3 replies
  • 643 views
  • 3 likes
  • 3 in conversation