Thank you in advance!
My code:
data have;
input id word $6.;
cards;
1 Butter
1 Butter
1 Arm
2 Pole
2 Pole
2 Arm
2 Train
;
run;
proc sql;
create table t1 as
select id, count(id) as n1
from have
group by id;
create table t2 as
select id, count (id),word as n2 from
have group by id,word;
create table t3 as
select id,count(id) as n3 from t2
group by id;
select t1.id,n1,n3 from t1,t3
where t1.id=t3.id;
quit;
id n1 n3
1 3 2
2 4 3
Check table t2, I don't think it is what you expected... anyway, you seem to want for each ID, the total number of words and the number of distinct words. You can indeed get that in one SQL step :
proc sql;
select id, count(word) as totalWords, count(distinct word) as distinctWords
from have
group by id;
PG
Check table t2, I don't think it is what you expected... anyway, you seem to want for each ID, the total number of words and the number of distinct words. You can indeed get that in one SQL step :
proc sql;
select id, count(word) as totalWords, count(distinct word) as distinctWords
from have
group by id;
PG
Nice! I've never seen the "count(distinct word)" syntax before!
Of course the following one-step data step approach is not as robust as PG's SQL solution, as it requires same word cluster together within the same id:
data want;
do until (last.word);
set have;
by id word notsorted;
if first.id then call missing(n1,n2);
n1+1;
end;
n2+1;
if last.id then output;
run;
Haikuo
Haikuo
is almost at the optimal solution
Perhaps this might work for ordered data
Data want ;
N1=0;
DO N2=1 by 1 UNTIL( last.ID );
set have ;
BY ID WORD;
N1 + last.WORD ;
END ;
run;
Of course it n
Message was edited by: Peter Crawford don't know why
Thank you very much!!!
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 25. Read more here about why you should contribute and what is in it for you!
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.