BookmarkSubscribeRSS Feed
Haydn
Quartz | Level 8

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;
3 REPLIES 3
KachiM
Rhodochrosite | Level 12

@Haydn 

 

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;
PeterClemmensen
Tourmaline | Level 20

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;
ballardw
Super User

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.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1047 views
  • 4 likes
  • 4 in conversation