Sunday, July 12, 2009

Automatic variables-first. ,last.

These automatic variables are not ‘always’ available for data step as other _n_ ,_error_,_character_, _numeric_ which are created for every data step.
First. And last. Are created during ‘by’ group processing only i.e when we use a by statement.first. has value 1 for first value of by group and 0 for the rest .similarly last. Is 1 for the last occurrence value in the by group and 0 for all other in that by group.
For example---
Consider the code---
Data data1;
Input id age month sal;
Cards;
001 23 110000
001 23 2 20000
002 21 1 10000
003 24 114000
003 24 2 13000
;
Run;
/*we want count of obs with duplicate id number*/
Data data2;
Set data1;
By id; /*first. And last. Created*/
If first. Then n_id=0; /* sets n_id 0 for new id value*/
N_id+1; /*increments by 1 for every recurrence of id*/
If last. Then output ; /*outputs on occurrence of last id in the group*/

Run;

Output of data2---
id age month sal n_id

1 23 2 20000 2
2 21 1 10000 1
3 24 2 13000 2

Send link to --

TopBlogDir.blogspot.com button Education Blog Directory Academic,  Learning & Educational Blogs - Blog Catalog Blog Directory SEO Court Directory myblog Visit blogadda.com to discover Indian blogs Add to Technorati Favorites Subscribe with Bloglines DigNow.net

Join My Community at MyBloglog!

Free Web Directory - Add Your Link
The Little Web Directory
Monster Directory A List Sites