DATA Step, Macro, Functions and more

Replicate a single row of data n times

Reply
Contributor
Posts: 20

Replicate a single row of data n times

Suppose I have the following dataset.

id var1 var2 var3.
1 1 4 6
2 7 4 0
3 1 9 6

How do I replicate each observation (1,2, and 3) twice so that the final output looks like....

1 1 4 6
1 1 4 6
2 7 4 0
2 7 4 0
3 1 9 6
3 1 9 6

One (inefficient) way of doing this is to make two identical datasets and merge them with a set statement. Is there a more efficient way of doing this.

Thanks a lot.

Message was edited by: DB_ECON Message was edited by: DB_ECON
Super Contributor
Super Contributor
Posts: 3,174

Re: Replicate a single row of data n times

Just hardcode two OUTPUT statements in a DATA step with your SET statement.

Scott Barry
SBBWorks, Inc.
SAS Super FREQ
Posts: 8,743

Re: Replicate a single row of data n times

Hi:
Regarding:
merge them with a set statement

The SET statement allows you to bring datasets together VERTICALLY, either:
1) stack datasets (dataset 1 has 3 obs, dataset 2 has 3 obs) the final dataset will have 6 obs, The first dataset listed on the SET statement will contribute the first group of observations; the dataset listed second on the SET statement will contribute the second group of observations.

2) interleave datasets -- with the above scenario, you would still end up with 6 obs, but in interleaved order.

The MERGE statement allows you to join datasets HORIZONTALLY -- so for example, if you have 3 obs in both datasets and all 3 obs in both datasets match on a BY variable, then the output dataset would have 3 observations and each observation would have the variables that came from both datasets.

But, as Scott pointed out, the simple solution is to use 2 OUTPUT statements. So you won't need to "merge with a set".

cynthia
Occasional Learner
Posts: 1

Re: Replicate a single row of data n times

Hi DB_ECON

 

You can do that very easily by just appending and then sorting them.

Suppose your dataset name is A and you need output in B

Program:-

 

data B;

set A A;

run;

proc sort data=B;

by ID;

run;

 

You will get your required result in B dataset.

Ask a Question
Discussion stats
  • 3 replies
  • 798 views
  • 0 likes
  • 4 in conversation