BookmarkSubscribeRSS Feed
dht115
Calcite | Level 5

Hi, 

I need to find how many times account_id is repeating along with department. 

I tried to use first. procedure for this problem. I was able to find how many times account_id is repeating; however, I was not able to get the desire value for department. 

Also, do we have proc sql procedure to do something similar in SAS? 

 

data have;
input Account_Id Dept Salary Emp_Status $ Projects $ Rating;
cards;
111         123  7000  Perm       A        5
111         123  6000  Perm       B        4
111         123  7000  Perm       C        5
222         124  6000  Perm       A        5
333         125  7000  Perm       B        4
333         125  7000  Perm       C        5
;
run;

proc sort data=have;
by account_id dept;
run;

data want ;
set have;
by account_id;
if first.account_id then do;
flag = 0;
if first.dept then flag_1 = 0;
end;
flag_1 + 1;

flag + 1;
retain flag_1;
retain flag;
run;

dht115_0-1680279314903.png

 

3 REPLIES 3
Kurt_Bremser
Super User

You get FIRST. variables only for variables included in the BY statement, so you must have dept there. Add the NOTSORTED option if "smaller" values can follow "larger" values.

PaigeMiller
Diamond | Level 26

Its usually unhelpful to show us the wrong answer, and not show us the correct answer. So show us the answer you want for this data.

 

If you need values that are incremented on each row subject to some conditions, SQL is not the tool.

 

 

--
Paige Miller
Tom
Super User Tom
Super User

I don't understand what you are counting.  Can you share the expected output for the given input?

 

Also I don't understand how your data is organized.  Are departments defined within accounts? Or are accounts defined within departments?

 

If you want to count how many times the combination of account and department changes and number each record within those combinations you could use something like this:

data want;
  set have;
  by Account_Id Dept ;
  department_no + first.dept;
  record_no+1;
  if first.dept then record_no=1;
run;

Result

       Account_                       Emp_                           department_    record_
Obs       Id       Dept    Salary    Status    Projects    Rating         no           no

 1        111       123     7000      Perm        A           5           1            1
 2        111       123     6000      Perm        B           4           1            2
 3        111       123     7000      Perm        C           5           1            3
 4        222       124     6000      Perm        A           5           2            1
 5        333       125     7000      Perm        B           4           3            1
 6        333       125     7000      Perm        C           5           3            2

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 441 views
  • 0 likes
  • 4 in conversation