BookmarkSubscribeRSS Feed
Mohamed
Calcite | Level 5
Dear,

I have two data set as follow:

one ID CNT
1 2
2 1

Two
ID Qnty Price


1 50 20
1 15 7
1 18 40
1 14 80
2 18 70
2 15 41

how can I select number of observations from data set two as per CNT from data set one. it means I want to select only two observations from data set two has ID 1. and only 1 observation from data set two has ID 2 Message was edited by: Mohamed
2 REPLIES 2
yonib
SAS Employee
Hi,
From Your description i dont understand, for example how you choose the two observations from data set two has ID 1???
In my soulotion i assumed you want to choose the first onces:

options symbolgen mprint;

data one;
input ID CNT;
cards
;
1 2
2 1
;
run;
*count the numbers of id's in table one;
proc sql noprint;
select count(*)
into: num
from one
;
quit;

proc sql noprint;
select id,cnt
into: var1-:var%sysfunc(compress(&num)),:cnt separated by '*'
from one
;
quit;


data Two;
input ID Qnty Price;
cards;
1 50 20
1 15 7
1 18 40
1 14 80
2 18 70
2 15 41
;
run;

%macro example;
%do i=1 %to #
proc sql;
create table _tosplit_&i as
select *
from two
where ID=&&var&i
;
quit;

data _tossplit_&i ;
set _tosplit_&i (obs=%scan(&cnt,&i,*));
run;
%if &i=1 %then %do;
data all;
set _tossplit_&i;
run;
%end;
%else %do;
proc append base=all
data=_tossplit_&i
;
run;
%end;

%end;



%mend examle;
%example;

Good Luck
oloolo
Fluorite | Level 6
change the variable name CNT in data one appropriately, and use PROC SURVEYSELECT with strata

check the manual of SURVEYSELECT

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 2 replies
  • 976 views
  • 0 likes
  • 3 in conversation