Help using Base SAS procedures

How to count obs

Reply
Frequent Contributor
Posts: 80

How to count obs

Hi,

How to count observations using data set? I explain.

In my table "have", there are 15 observation with duplicated values (by transaction and product)

I need to get a sample (only 3 observation) per product.

How to do that?

Super Contributor
Posts: 297

Re: How to count obs

Please provide a sample of the data you are working with and an example of the desired output.

Frequent Contributor
Posts: 80

Re: How to count obs

Hi Scott, thanks..

in my table i have

id  financed product

1   123,67   car

1   145,90   car

1  1214,00  bike

1  1234,00  car

1  1534,00  bike

1  1094,00  car

1  1834,00  bike

1  9834,00  bike

1  1954,00  car

1  9886,00  bike

want

1   123,67   car

1   145,90   car

1  1234,00  car

1  1214,00  bike

1  1534,00  bike

1  1834,00  bike


so i need only 3 observation per product

Super Contributor
Posts: 297

Re: How to count obs

Hi Augusto,

How about PROC SURVEYSELECT?

DATA HAVE;

INFILE DATALINES DLM=',';

INPUT ID FINANCED PRODUCT $;

DATALINES;

1,123.67,CAR

1,145.90,CAR

1,1214.00,BIKE

1,1234.00,CAR

1,1534.00,BIKE

1,1094.00,CAR

1,1834.00,BIKE

1,9834.00,BIKE

1,1954.00,CAR

1,9886.00,BIKE

;

RUN;

PROC SORT DATA=HAVE;

BY PRODUCT;

RUN;

PROC SURVEYSELECT DATA=HAVE SAMPSIZE=3 OUT=WANT SEED=0;

STRATA PRODUCT;

RUN;

Contributor
Posts: 70

Re: How to count obs

Hi..

You can simply do using datastep.. as below..

DATA HAVE;

INFILE DATALINES DLM=',';

INPUT ID FINANCED PRODUCT $;

DATALINES;

1,123.67,CAR

1,145.90,CAR

1,1214.00,BIKE

1,1234.00,CAR

1,1534.00,BIKE

1,1094.00,CAR

1,1834.00,BIKE

1,9834.00,BIKE

1,1954.00,CAR

1,9886.00,BIKE

;

RUN;

PROC SORT DATA=HAVE;

BY PRODUCT;

RUN;

data want;

set have;

by product;

if first.product then n = 0;

n+1;

if n in (1, 2, 3) then output;

drop n;

run;

any questions ? ..

Thanks & Regards,

yaswanth J.

Contributor
Posts: 70

Re: How to count obs

If you don`t want to mention the numbers like (1,2,3) you can write the code as follow,

data want;

set have;

by product;

if first.product then n = 0;

n+1;

if n < 4 then output;

drop n;

run;

Ask a Question
Discussion stats
  • 5 replies
  • 238 views
  • 3 likes
  • 3 in conversation