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

Hi all,

 

I am working on a sas dataset, where I need to get the count of certain statuses ('3' or '6').

My input data is:

 

loan_id  month STAT 

1234     1         0     

1234     2         0 

1234     3         3 

1234     4         6 

1234     5         6

 

Output should be:

loan_id  month STAT count

1234     1         0        0

1234     2         0        0

1234     3         3        1

1234     4         6        2 

1234     5         3        3

 

Whenever the status is either 3 or 6, the count should be incremented.

Please help me with this.

 

Thanks

   

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

This version assumes you want a separate count for each LOAN_ID:

 

data want;

set have;

by loan_id;

if first.loan_id then count=0;

if stat in (3, 6) then count + 1;

run;

 

View solution in original post

2 REPLIES 2
Astounding
PROC Star

This version assumes you want a separate count for each LOAN_ID:

 

data want;

set have;

by loan_id;

if first.loan_id then count=0;

if stat in (3, 6) then count + 1;

run;

 

HB
Barite | Level 11 HB
Barite | Level 11

As an alternative and if you are wanting more of a summary, you could use PROC SQL

 

data loan_status;
   input loan_id  month STAT ;
datalines;
1234    1	0
1234    2   0
1234    3   3
1234    4   6
1234    5   6
2345	1	3
2345	2	6
2345	3	0
3425	4	6
3423	5	0
;
run;


proc sql;
	create table status_count as
	select loan_id, count(stat) as mycount
	from loan_status
	where stat in (3,6)
	group by loan_id
	order by loan_id;
quit;

This would give you

 

The SAS System  
   
loan_id mycount
1234 3
2345 2
3425 1

 

Just presenting an alternative.

 

 

 

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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
  • 2 replies
  • 563 views
  • 0 likes
  • 3 in conversation