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.

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 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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