Help using Base SAS procedures

How to Grab observations with certain count of any variable

Reply
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 .

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

Sarvendra

Super User
Posts: 5,438

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

Posted in reply to Sarvendra

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

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;

but it was giving error.

Hoping for soon reply

Sarvendra

Super User
Super User
Posts: 7,076

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

Posted in reply to Sarvendra

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 Smiley Happy

Ask a Question
Discussion stats
  • 4 replies
  • 205 views
  • 0 likes
  • 3 in conversation