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

Hi SAS wizards,

 

I have a list of customers and 24 month columns(mo1, mo2...) in front of each. Each can take values from 1 to n. However this 'n' is unique for each row only. For e.g. for customer A (say n=4), mo1=1, mo2=2, mo3=2, mo4=2, mo5=3, mo6=4 and so on til mo24. I want to count how many times this value changes across the 24 months for each customer (i.e. in this 6 month period in the example, the count would be 3). I understand the logic of doing it but don't how to code it in using macros/data step/loops etc in SAS.

 

The logic I have is:

  1. Initiate a counter at 0 and a ref as mo1 value.
  2. Check this ref vs mo2, mo3 etc sequentially and increase counter when value is different while also updating ref as the new value.
  3. Return the Counter value at the end for each row/customer.
1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

Use an ARRAY in a DATA step

 

UNTESTED CODE

data want;
    set have;
    array mo mo1-mo24;
    count=0;
    do i=2 to 24;
         if mo(i)^=mo(i-1) then count=count+1;
    end;
    drop i;
run;
--
Paige Miller

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26

Use an ARRAY in a DATA step

 

UNTESTED CODE

data want;
    set have;
    array mo mo1-mo24;
    count=0;
    do i=2 to 24;
         if mo(i)^=mo(i-1) then count=count+1;
    end;
    drop i;
run;
--
Paige Miller
Recker96
Calcite | Level 5

This works exactly how I wanted it to. Thanks a lot PaigeMiller!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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