display records based on numbers in other dataset

Reply
Contributor
Posts: 62

display records based on numbers in other dataset

I have two datasets "A" and "B".

data a;

name age frnds;

raj 12 a

raj 12 b

raj 12 c

rahul 21 a

rahul 21 d

rahul 21 e

rahul 21 f

raj 21 f

raj 21 h...... .... .....

;

like this there are many observations in my data set A.And data set B like

data B;

name age frnds;

raj     12       4

rahul 21   3

raj     21    5

;

so from data set "A" I want that much observations as there are numbers in dataset b in "frnds".For ex in data set b for "raj" "12"

there are "4" in frnds so I need first 4 observations from data set A .Similarly for rahul need first 3 observations from data set A

and 5 observations for "raj" "21" from data set A and so on.

 

Final op

name age frnds

raj 12       a

raj 12       b

raj 12       c

raj 12       d  

raj 12       e 

rahul 21   a

rahul 21   f

rahul 21   g

 

and so on.......

Super User
Super User
Posts: 7,677

Re: display records based on numbers in other dataset

Well, I would say that this seems to be a pointless endeavour, you have the data you have, why try to create data which isn't there.  Anyway, it can be done:

data datab (drop=frnds);
  set datab;
  do i=1 to frnds;
    output;
  end;
run;

proc sql;
  create table WANT as
  select coalesce(A.NAME,B.NAME) as NAME,
         coalesce(A.AGE,B.AGE) as AGE,
         A.FRNDS
  from   DATAA A
  full join DATAB B
  on     A.NAME=B.NAME 
  and    A.AGE=B.AGE;
quit;
  
    

What this does is expand datab to have as many rows as stipulated in frnds variable.  This is then joined onto dataa, using an all to all join, to expand dataa to have the same number of records as datab.

Super User
Posts: 9,856

Re: display records based on numbers in other dataset


data a;
input name $ age frnds $;
cards;
raaj 12 a
raaj 12 b
raaj 12 c
rahul 21 a
rahul 21 d
rahul 21 e
rahul 21 f
raj 21 f
raj 21 h
;
run;
data B;
input name $ age count;
cards;
raaj     12       4
rahul 21   3
raj     21    5
;
run;
data want;
 merge a b;
 by name age;
 if first.age then n=0;
 n+1;
 if n le count;
 drop n count;
run;

Ask a Question
Discussion stats
  • 2 replies
  • 244 views
  • 1 like
  • 3 in conversation