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:
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;
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;
This works exactly how I wanted it to. Thanks a lot PaigeMiller!
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.