DATA Step, Macro, Functions and more

How to grab observations with particular count of any value of variable

Reply
Contributor
Posts: 20

How to grab observations with particular count of any value of variable

I have a dataset with ACCNO  as a variable.

we need to grab all the observations with  ACCNOs with COUNT 3 or more .(or to create a new dataset with COUNT 3 or more)

I was thinking to implement this logic using first and last temporary variables but it was giving error.

When it will read the first observation of any individual account number then initialize n=0 and increment each  iterating time and when it will read last observation of any individual account then if value of n greator than or equal to 3 the it should

Create a new dataset satisfying this condition.

Can there be any better  logic for this .as below logic not working

data ssingh.FINADVNEWariable

set ssingh.FINADV

by ACCNO;

if first.ACCNO then n = 0;

n + 1;

if last.ACCNO and n gt 3 then output ;

                                                                          

run;

Please reply

Super User
Posts: 19,810

Re: How to grab observations with particular count of any value of variable

Posted in reply to Sarvendra

A Proc SQL step works nicely (untested)

proc sql;

     create table want as

select * from

have where accno in (select accno from have group by accno having count(accno)>3);

quit;

Contributor
Posts: 20

Re: How to grab observations with particular count of any value of variable

Thanks a lot Reeza its working absolutely fine. Smiley Happy Smiley Happy

Super User
Posts: 3,254

Re: How to grab observations with particular count of any value of variable

Posted in reply to Sarvendra

Your code has syntax errors. You are missing semicolons on the first two lines:

data ssingh.FINADVNEWariable;

set ssingh.FINADV;

Otherwise your code should work as long as the dataset on the SET statement is sorted.

Super User
Posts: 5,505

Re: How to grab observations with particular count of any value of variable

Perhaps a smaller issue ...

Your original post asks for count greater than or equal to 3.

gt means "greater than"

ge means "greater than or equal"

Contributor
Posts: 20

Re: How to grab observations with particular count of any value of variable

Thanks it was just a syntax error of semicolumn Smiley Happy

Super Contributor
Posts: 307

Re: How to grab observations with particular count of any value of variable

Posted in reply to Sarvendra
Ask a Question
Discussion stats
  • 6 replies
  • 315 views
  • 0 likes
  • 5 in conversation