Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to count? 2

Status
Not open for further replies.

saqi2000

Programmer
Apr 11, 2002
84
GB
I am quite new to SAS and I need help with the code below. Please find the code below I am trying to count the number of unique employees but it prints employees id and then finally prints number of employee at the end.

please help

data work.employees;
set tm.employees;
run;
/*sort work.employee by empid and output unique empids into unique_emps data set.*/
proc sort in=work.employees out=work.unique_emps nodupkey;
by empid;
run;

/*display empid number and Print total number of observations */
proc print data=work.unique_emps n='Total number of observations: '
noobs;
var empid;

title 'Number of unique employee that have granted bonus';

run;

thanks in advance

[3eyes][3eyes][3eyes]saqi[3eyes][3eyes][3eyes]

 
Hi saqi,

in case you just want the number and no printing, you can have it (not the most 'clever' way) by reading the _n_ variable:

Code:
177   data _null_;  /*--  so no table gets created  */
178     set unique_emps end=last;
179     if last then do;
180        /*--  have it in macro variable  */
181        call symput ('NUMEMPS', put (_n_, 8.));
182     end;
183   run;

NOTE: There were 48 observations read from the data set WORK.UNIQUE_EMPS.
NOTE: DATA statement used:
      real time           0.03 seconds
      cpu time            0.01 seconds


184   %put Distinct employees: &NUMEMPS.;
Distinct employees:       48
185
186   data NumEmps;  /*--  or into a table  */
187     set unique_emps end=last;
188     if last then do;
189       output;
190     end;
191   run;

NOTE: There were 48 observations read from the data set WORK.UNIQUE_EMPS.
NOTE: The data set WORK.NUMEMPS has 1 observations and 17 variables.
NOTE: DATA statement used:
      real time           0.04 seconds
      cpu time            0.00 seconds

Disadvantage is that you read the complete dataset, so gets annoying when you get to biggy tables. I remember a macro that didn't read a single obs, have it at work. In case you're interested, I can try to dig that up.

Cheers,
Matthias
 
There are a number of ways that you could count the distinct employees. Is this an HW question?
Do you want to print the results somewhere. I noticed that you use a proc print. Does that mean that you want some sort of results printed? Would the log do?
You could do this...
proc sort
data = work.employees
out = work.unique_emps nodupkey;
by empid;
run;
** THIS WILL GIVE A LIST OF UNIQUE EMPLOYEES;
data _null_;
dsid = open('work.unique_emps','i');
mobs = attrn(dsid,'nobs');
put "The number of employees at this company: " mobs;
**THE ABOVE STATEMENT SEND THIS INFORMATION TO THE LOG;

run;
if this doesnt help say so and 'cause I have several more ways.
 
Guys,

thanks very much for your help. I mistyped my question it should have been number of employees for each department.

with apology

Saqi
 
In that case if your dataset is uniq at empid then just simply do following..
proc freq data=work.unique_emps;
table department/list missing;
run;

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top