Hi Friends,
I need your help ,in the below situation.
I have a char type variable as number, I need to find the total count in the variable ,and unique count( with out duplication) and how many numbers are there same.
For instance
x is char
x= 1234567849
I need to find total count = 10
unique count = 9 (because 4 repeated twice)
one number is matching i.e 4
Kindly help on the above.Thanks you very much.
Regards,
Dhruva
Do you just have the one value or do you need to do this for multiple values?
Perhaps something like this
data have;
x="1234567849";
run;
data temp(keep=y);
set have;
do i=1 to length(x);
y=char(x, i);
output;
end;
run;
proc sql;
create table want as
select count(*) as totalcount,
count(distinct y) as uniquecount
from temp;
quit;
This little example covers how to count distinct and totals.
/*This demonstrates how to count the number of unique occurences of a variable
across groups. It uses the SASHELP.CARS dataset which is available with any SAS installation.
The objective is to determine the number of unique car makers by origin/
Note: The SQL solution can be off if you have a large data set and these are not the only two ways to calculate distinct counts.
If you're dealing with a large data set other methods may be appropriate.*/
*Count distinct IDs;
proc sql;
create table distinct_sql as
select origin, count(distinct make) as n_make
from sashelp.cars
group by origin;
quit;
*Double PROC FREQ;
proc freq data=sashelp.cars noprint;
table origin * make / out=origin_make;
run;
proc freq data=origin_make noprint;
table origin / out= distinct_freq;
run;
title 'PROC FREQ';
proc print data=distinct_freq;
run;
title 'PROC SQL';
proc print data=distinct_sql;
run;
@dhruvakumar wrote:
Hi Friends,
I need your help ,in the below situation.
I have a char type variable as number, I need to find the total count in the variable ,and unique count( with out duplication) and how many numbers are there same.
For instance
x is char
x= 1234567849
I need to find total count = 10
unique count = 9 (because 4 repeated twice)
one number is matching i.e 4
Kindly help on the above.Thanks you very much.
Regards,
Dhruva
To me, this is simple and direct:
data want;
set have;
array c {0:9} c_0 - c_9;
do k=1 to length(charvar);
digit = input(substr(charvar, k, 1), 1.);
if (0 <= digit <= 9) then c{digit} = sum(c{digit}, 1);
end;
total_count = sum(of c{*});
unique_count = n(of c{*});
drop k;
run;
HI,
Thanking you.Is there any way to find matching repeated number count, for instance in the above example 4 is repeated twice so counted should be 2.
Regards,
Dhruva
It's already in there. Try printing the data, and look at c_0 through c_9. The count for "4" will be contained in c_4 (although counts of zero are not shown ... those variables will be missing instead of 0.)
data have;
x="1234567849";
run;
data want;
if _n_=1 then do;
length k $ 1;
declare hash h();
h.definekey('k');
h.definedone();
end;
set have;
count=lengthn(x);
do i=1 to count;
k=char(x,i);h.ref();
end;
unique_count=h.num_items;
drop k i;
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.