Contributor
Posts: 20

# How to Grab observations with certain count of any variable

I have a dataset with ACCNOs  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 .

by ACCNO;

if first.ACCNO then n = 0;

n + 1;

if last.ACCNO and n gt 3 then output ;

run;

Sarvendra

Super User
Posts: 5,884

## Re: How to Grab observations with certain count of any variable

Do you want to grab all observations for an ACCNO that are => 3? Or just the last observation for each ACCNO if that one one is => 3.

Depending on your requirement, a group by and having in SQL seems simpler.

Data never sleeps
Contributor
Posts: 20

## Re: How to Grab observations with certain count of any variable

Hi Linush

I want to grab those observations of ACCNO for those individual accno for which count =>3.

Can you please give the logic in form of some dummy code.i have tried to implement

by ACCNO;

if first.ACCNO then n = 0;

n + 1;

if last.ACCNO and n gt 3 then output ;

run;

but it was giving error.

Sarvendra

Super User
Posts: 8,127

## Re: How to Grab observations with certain count of any variable

What error did you get?  Note that you are missing semi-colons at the end of your DATA and SET statements.

Contributor
Posts: 20

## Re: How to Grab observations with certain count of any variable

Thanks Tom it was syntax error

Discussion stats
• 4 replies
• 224 views
• 0 likes
• 3 in conversation