BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Solly7
Pyrite | Level 9

Hi I need help in counting the distinct policy_no, where saved=1 as well as counting number of all distinct policies..see below data HAVE and WANT

 

 

data HAVE;
input Saved policy_no user : $20.;
datalines;
1 1 ALUPIYA
1 1 ALUPIYA
0 2 ALUPIYA
1 3 ALUPIYA
0 5 DLOUIS
1 4 DLOUIS
1 4 DLOUIS
1 6 DMORAKE
1 7 DMORAKE
1 8 DMORAKE
0 9 DMORAKE

;

data WANT;
input Total_Saved Total_policies user : $20.;
datalines;
2 3 ALUPIYA
1 2 DLOUIS
3 4 DMORAKE
;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Try this:

data want;
retain total_saved total_policies sv;
set have;
by user notsorted policy_no;
if first.user
then do;
  total_saved = 0;
  total_policies = 0;
end;
if first.policy_no
then do;
  total_policies + 1;
  sv = 0;
end;
sv = max(sv,saved);
if last.policy_no then total_saved + sv;
if last.user;
drop sv saved policy_no;
run;

View solution in original post

4 REPLIES 4
Kurt_Bremser
Super User

Try this:

data want;
retain total_saved total_policies sv;
set have;
by user notsorted policy_no;
if first.user
then do;
  total_saved = 0;
  total_policies = 0;
end;
if first.policy_no
then do;
  total_policies + 1;
  sv = 0;
end;
sv = max(sv,saved);
if last.policy_no then total_saved + sv;
if last.user;
drop sv saved policy_no;
run;
Solly7
Pyrite | Level 9
Thanks a lot Kurt, it works perfectly fine!
Solly7
Pyrite | Level 9
Hi Kurt, i have other variables other than those ones i have mentioned above that i need to include , for example I have Variable user_type(I ,O R)..how do I include it within the final dataset WANT?
Kurt_Bremser
Super User

Since you do have some kind of summary in this, you need to decide how you do that for additional variables. You will need more retained variables to keep the state you want.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 4 replies
  • 848 views
  • 1 like
  • 2 in conversation