BookmarkSubscribeRSS Feed
DB_ECON
Calcite | Level 5
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
4 REPLIES 4
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
Just hardcode two OUTPUT statements in a DATA step with your SET statement.

Scott Barry
SBBWorks, Inc.
Cynthia_sas
SAS Super FREQ
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
divyanksingh12
Calcite | Level 5

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.

yifan88
Calcite | Level 5

You can try the following code( as single is your original data set )

data new ;
set single;
do i=1 to 2;
output;
end;
run;
proc print data=new(drop=i) noobs;
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!

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
  • 4 replies
  • 9808 views
  • 0 likes
  • 5 in conversation