Counting the number of observations in a BY-Group


/ Published in: SAS
Save to your folder(s)

Determine how many observations there are in each BY-Group by using BY-Group processing.


Copy this code and paste it in your HTML
  1. /* Create sample data */
  2.  
  3. data old;
  4. input state $ accttot;
  5. datalines;
  6. ca 7000
  7. ca 6500
  8. ca 5800
  9. nc 4800
  10. nc 3640
  11. sc 3520
  12. va 4490
  13. va 8700
  14. va 2850
  15. va 1111
  16. ;
  17.  
  18. proc sort data=old;
  19. by state;
  20. run;
  21.  
  22. /* To get the number of observations in each group of states, start */
  23. /* a counter on the first observation of each BY-Group. The last */
  24. /* observation in the BY-Group contains the total number of */
  25. /* observations and is output to the data set. */
  26.  
  27. data new;
  28. set old (drop= accttot);
  29. by state;
  30. if first.state then count=0;
  31. count+1;
  32. if last.state then output;
  33. run;
  34.  
  35. proc print;
  36. run;
  37.  
  38. /* Alternative approach using PROC FREQ to generate the same output */
  39.  
  40. proc freq;
  41. tables state / out=new(drop=percent);
  42. run;

URL: http://support.sas.com/kb/24/595.html

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.