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

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..

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Shmuel
Garnet | Level 18

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;

View solution in original post

5 REPLIES 5
Shmuel
Garnet | Level 18

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;

Astounding
PROC Star

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;

 

celdelmas
Calcite | Level 5

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

Shmuel
Garnet | Level 18

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;

Astounding
PROC Star

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.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1262 views
  • 0 likes
  • 3 in conversation