DATA Step, Macro, Functions and more

how to multipy a variable

Accepted Solution Solved
Reply
Contributor
Posts: 20
Accepted Solution

how to multipy a variable

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

 

 


Accepted Solutions
Solution
‎12-08-2016 07:25 AM
Trusted Advisor
Posts: 1,401

Re: how to multipy a variable

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


All Replies
Trusted Advisor
Posts: 1,401

Re: how to multipy a variable

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;

Super User
Posts: 5,093

Re: how to multipy a variable

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;

 

Contributor
Posts: 20

Re: how to multipy a variable

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

Solution
‎12-08-2016 07:25 AM
Trusted Advisor
Posts: 1,401

Re: how to multipy a variable

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;

Super User
Posts: 5,093

Re: how to multipy a variable

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.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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