Hi all,
I have a dataset with one column (variable X) and 10000 lines. I need to create a dataset including this variable 6 times. Therefore, I would like to create a variable named N that could take 6 values (1 to 6) and associate the 10000 lines of X to each value of N.
I am not familiar with this type of code, would somoeone could help me to sort it out?
Thank you very much..
data want;
set have(in=N1)
have(in=N2)
have(in=N3)
have(in=N4)
have(in=N5)
have(in=N6)
;
if N1 then varN = 'N1'; else
if N2 then varN = 'N2'; else
if N3 then varN = 'N3'; else
if N4 then varN = 'N4'; else
if N5 then varN = 'N5'; else
if N6 then varN = 'N6';
run;
It is not very clear what you have and what you want.
Let's look at one line with x=1;
to you want the result line look like: x=1; n1=1; n2=1 ... n6=1; ?
If so just do:
data want;
set have;
array nx n1-n6;
do i=1 to 6; nx(i) = x; end;
drop i;
run;
It's easy enough to do:
data want;
set have;
do n=1 to 6;
output;
end;
run;
However, once you do this it becomes nearly impossible to sort the data and then get it back into its original order. You might want to add another variable:
data want;
set have;
original_line_number = _n_;
do n=1 to 6;
output;
end;
run;
Thank you both for your responses.
This is not what I was looking for, let me explain better:
My data set (HAVE) is:
varX (quantitative)
x1
x2
x3
.
.
.x10000
(one variable, 10000 values).
I need to "copy paste" 6 times this dataset using a new variable N.
dataset (WANT) to obtain (60000lines)
varN varX
n1 x1
n1 x..
n1 x10000
n2 x1
n2 x..
n2 x10000
.
.
n6 x1
n6 x..
n6 x10000
I hope this is clearer..
Thank you very much for your help
data want;
set have(in=N1)
have(in=N2)
have(in=N3)
have(in=N4)
have(in=N5)
have(in=N6)
;
if N1 then varN = 'N1'; else
if N2 then varN = 'N2'; else
if N3 then varN = 'N3'; else
if N4 then varN = 'N4'; else
if N5 then varN = 'N5'; else
if N6 then varN = 'N6';
run;
You'll have to show us the calculations. How do you get from x1 to x.. and x10000?
WIthout that knowledge, here's the form to the program:
data want;
set have;
varN=_n_;
output;
varX= ... some formula to replace varX with x.. ...;
output;
varX= ... some formula to replace varX with x10000;
output;
run;
For this to have a chance of working, varX cannot change type. For example, if it is numeric to start, all its values must remain numeric.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.
Ready to level-up your skills? Choose your own adventure.