I'm trying to return a value of 1 for each unique occurrence of Ref_numb
I've tried the below and I'm getting a result, but my unique customer count is 18 less than what I'm expecting.
Is there something I'm missing with the code below?
Proc sort data=work.testdata ;
by Ref_Numb;
run;
data work.test_Op ;
set work.testdata ;
by Ref_Numb ;
if first.Ref_Numb then UniqCustomer = 1;
run;
Do you want 1 for each distinct ID? Then how to count the number of distinct values? I show a simple example and see whether it helps you.
data have;
input id x;
datalines;
10 23
10 12
10 11
20 30
20 40
30 12
;
run;
proc sort data = have;
by id;
run;
data want;
set have;
by id;
if first.id then uniqC + 1;
put uniqC =;
run;
Do you simply want a value of 1 on the first occurrence of a customer? If so,
data have;
input Ref_Numb num;
datalines;
2 1
1 4
2 2
1 3
3 6
1 5
;
data want;
if _N_=1 then do;
declare hash h();
h.definekey('Ref_Numb');
h.definedone();
end;
set have;
if h.check() ne 0 then do;
UniqCustomer=1;
h.add();
end;
run;
Example input data and the desired output for that data goes a long ways to avoiding multiple questions about this case, that case and what was intended.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.