BookmarkSubscribeRSS Feed
Augusto
Obsidian | Level 7

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?

5 REPLIES 5
Scott_Mitchell
Quartz | Level 8

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

Augusto
Obsidian | Level 7

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

Scott_Mitchell
Quartz | Level 8

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;

yaswanthj
Calcite | Level 5

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.

yaswanthj
Calcite | Level 5

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 900 views
  • 3 likes
  • 3 in conversation