Hello everyone,
In a dataset, I want to copy some rows base on condition,
such as in SAShelp.class,i want make copy of rows whose age is 15,
that means in the following dataset,I want rows 8,14,17,19(Janet,Mary,Ronald,William) ocurr twice(and maybe three times).
Thanks!
Mike
Original dataset:
Obs Name Sex Age Height Weight
1 Alfred M 14 69.0 112.5
2 Alice F 13 56.5 84.0
3 Barbara F 13 65.3 98.0
4 Carol F 14 62.8 102.5
5 Henry M 14 63.5 102.5
6 James M 12 57.3 83.0
7 Jane F 12 59.8 84.5
8 Janet F 15 62.5 112.5
9 Jeffrey M 13 62.5 84.0
10 John M 12 59.0 99.5
11 Joyce F 11 51.3 50.5
12 Judy F 14 64.3 90.0
13 Louise F 12 56.3 77.0
14 Mary F 15 66.5 112.0
15 Philip M 16 72.0 150.0
16 Robert M 12 64.8 128.0
17 Ronald M 15 67.0 133.0
18 Thomas M 11 57.5 85.0
19 William M 15 66.5 112.0
dataset needed:
Obs Name Sex Age Height Weight
Alfred M 14 69.0 112.5
Alice F 13 56.5 84.0
Barbara F 13 65.3 98.0
Carol F 14 62.8 102.5
Henry M 14 63.5 102.5
James M 12 57.3 83.0
Jane F 12 59.8 84.5
Janet F 15 62.5 112.5
Janet F 15 62.5 112.5
Jeffrey M 13 62.5 84.0
John M 12 59.0 99.5
Joyce F 11 51.3 50.5
Judy F 14 64.3 90.0
Louise F 12 56.3 77.0
Mary F 15 66.5 112.0
Mary F 15 66.5 112.0
Philip M 16 72.0 150.0
Robert M 12 64.8 128.0
Ronald M 15 67.0 133.0
Ronald M 15 67.0 133.0
Thomas M 11 57.5 85.0
William M 15 66.5 112.0
William M 15 66.5 112.0
Is this what you want?
data class;
set sashelp.class(where=(age=15));
data want;
set sashelp.class class;
by name;
proc print;run;
obs Name Sex Age Height Weight
1 Alfred M 14 69.0 112.5
2 Alice F 13 56.5 84.0
3 Barbara F 13 65.3 98.0
4 Carol F 14 62.8 102.5
5 Henry M 14 63.5 102.5
6 James M 12 57.3 83.0
7 Jane F 12 59.8 84.5
8 Janet F 15 62.5 112.5
9 Janet F 15 62.5 112.5
10 Jeffrey M 13 62.5 84.0
11 John M 12 59.0 99.5
12 Joyce F 11 51.3 50.5
13 Judy F 14 64.3 90.0
14 Louise F 12 56.3 77.0
15 Mary F 15 66.5 112.0
16 Mary F 15 66.5 112.0
17 Philip M 16 72.0 150.0
18 Robert M 12 64.8 128.0
19 Ronald M 15 67.0 133.0
20 Ronald M 15 67.0 133.0
21 Thomas M 11 57.5 85.0
22 William M 15 66.5 112.0
23 William M 15 66.5 112.0
Linlin
Whether the names are sorted or not, you can do it like this:
data want;
set sashelp.class;
output;
if age=15 then output; /* and repeat this line, if you want an additional copy */
run;
Good luck.
The base code was stolen from Astounding: using Macro to generalize your task:
%macro cpy (dname=,var=,i=);
data want;
set &dname;
output;
if age=&var then
do _n_=1 by 1 to &i;
output;
end;
run;
%mend;
%cpy (dname=sashelp.class,var=15,i=2)
Haikuo
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.
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.