BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
jmbnzhu
Calcite | Level 5


I have  two data sets A and B; A has 1000 observations and B has 40 observations. They have no commom variables. I want to create a data set C that has 1000x40 observations, for each observation of data A having 40 observations of data B. My code is below:

Data C;

do i=1 to 1000;

  set A;

do j=1 to 40

set B;

output;

end;

end;

Run;

Data c has only 40 observations. The log display: only 2 observations from data A; 40 observations from B;

Can you help me to fix this problem? Thank you,

Zhu

1 ACCEPTED SOLUTION

Accepted Solutions
jmbnzhu
Calcite | Level 5

data c;

set data A;

do i=1 to n;

set data B nobs=n point=i;

output;

end;

run;

View solution in original post

2 REPLIES 2
jmbnzhu
Calcite | Level 5

data c;

set data A;

do i=1 to n;

set data B nobs=n point=i;

output;

end;

run;

Haikuo
Onyx | Level 15

Looks like you have it figured out yourself by using a classic data step way. Although I think it is worth mentioning that for this kind of task, SQL seems to have a native edge, as SQL join generates Cartesian products to start with.

proc sql;

create table c as

select * from a,b;

quit;

BTW, with the advent of Hash(), data step has been granted the similar power as SQL join:

data c (drop=_:);

  if _n_=1 then do;

  set a (obs=1);

dcl hash h(dataset:'a', ordered:'a');

h.definekey(all:'y');

h.definedone();

dcl hiter hi('h');

  end;

  set b;

  _rc=hi.first();

  do while (_rc=0);

output;

_rc=hi.next();

end;

run;

Kindly regards,

Haikuo

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 559 views
  • 0 likes
  • 2 in conversation