DATA Step, Macro, Functions and more

Making 1 variable from 4 variables

Reply
Contributor
Posts: 23

Making 1 variable from 4 variables

HI. Can someone please help me? 

 

I have 20 simulations (s1-s20). I want to divide them into quartiles and make them into one variable.  Nothing I have tried works.  I have tried everything, even arrays. I looked on the web endlessly.  
 
Maybe you can help me. 
 

/* I want to divide 20 simulations by quartiles and put them into 1 variable
 where
 value group 1='Sim1-5 mean'
                      2='Sim6-Sim10 mean'
                     3='Sim11-Sim15 mean'
                    4='Sim16-Sim20 mean';
*/

I know to get the mean and make them into separate variables, but I cant' figure out how to make them into 1 variable so I can do a repeated ANOVA. 
 
 
I can get the averages as below from this code:
 

/*this works*/
tquart1g=sum(of s1-s5); av_quart1=tquart1g/5;
tquart2g=sum(of s6-s10); av_quart2=tquart2g/5;
tquart3g=sum(of s11-s15); av_quart3=tquart3g/5;
tquart4g=sum(of s15-s20); av_quart4=tquart4g/5;
run;
 
 
HOW do I make them into 1 VARIABLE? 
 
Thanks, Marysia
Respected Advisor
Posts: 2,989

Re: Making 1 variable from 4 variables

[ Edited ]

Show us a portion of your data that you want to work with. Show us the desired output. 

--
Paige Miller
Contributor
Posts: 23

Re: Making 1 variable from 4 variables

Posted in reply to PaigeMiller

Paige, 

First, thank you. 

The PG's first code worked (Thanks!), but the other part did not. 

 

So I have 20 tests for each participant. 

 

They were grouped into Time1-Time5, Time6-Time10, Time11-Time15, Time16 to Time20

So I have the averages for each participant for each time frame (4 periods as above). 

I want to get the following:

 

The output would be:

VAR Quart

(a proc print first)

 

variable row =   id  quart1  quart2  quart3  quart4             (and the numbers would be average of Time1-Time5 for quart1, etc)

                          1     56          67       76       87

                          2     32          50       63       98               (the highest score is 5)

                          .

                          .

                         n

 

That is what I'm going for so I can use that one variable in a repeated ANOVA to determine if the scores improved over time. 

 

Thank you. 

 

 

 

 

Super User
Posts: 23,683

Re: Making 1 variable from 4 variables


@mgrzyb wrote:

 

 
 
I can get the averages as below from this code:
 

/*this works*/
tquart1g=sum(of s1-s5); av_quart1=tquart1g/5;
tquart2g=sum(of s6-s10); av_quart2=tquart2g/5;
tquart3g=sum(of s11-s15); av_quart3=tquart3g/5;
tquart4g=sum(of s15-s20); av_quart4=tquart4g/5;
run;
 
 
HOW do I make them into 1 VARIABLE? 
 
Thanks, Marysia

That would get you the averages of a single row, not for multiple rows, which is rarely what you want. The answer to your question depends entirely on your data structure, but since we can't see your data it's impossible to say at the moment. 

If you show some sample data that explains what you have and what you want we can help with code. It does not have to be real data, fake data that has the same structure as yours is fine. Please post it as plain text as minimum, and not as an image though, no one wants to have to type out your data. 

 

 

Combining/Transforming data is commonly asked, so I suspect you need to use different search terms, but it can be hard to even know what to search sometimes. 

 


@mgrzyb wrote:

HI. Can someone please help me? 

 

I have 20 simulations (s1-s20). I want to divide them into quartiles and make them into one variable.  Nothing I have tried works.  I have tried everything, even arrays. I looked on the web endlessly.  
 
Maybe you can help me. 
 

/* I want to divide 20 simulations by quartiles and put them into 1 variable
 where
 value group 1='Sim1-5 mean'
                      2='Sim6-Sim10 mean'
                     3='Sim11-Sim15 mean'
                    4='Sim16-Sim20 mean';
*/

I know to get the mean and make them into separate variables, but I cant' figure out how to make them into 1 variable so I can do a repeated ANOVA. 
 
 
I can get the averages as below from this code:
 

/*this works*/
tquart1g=sum(of s1-s5); av_quart1=tquart1g/5;
tquart2g=sum(of s6-s10); av_quart2=tquart2g/5;
tquart3g=sum(of s11-s15); av_quart3=tquart3g/5;
tquart4g=sum(of s15-s20); av_quart4=tquart4g/5;
run;
 
 
HOW do I make them into 1 VARIABLE? 
 
Thanks, Marysia

 

Esteemed Advisor
Posts: 5,521

Re: Making 1 variable from 4 variables

I suspect you may want:

 

quart = 1; quartMean = mean(of s1-s5); output;
quart = 2; quartMean = mean(of s6-s10); output;
quart = 3; quartMean = mean(of s11-s15); output;
quart = 4; quartMean = mean(of s16-s20); output;


PG
Contributor
Posts: 23

Re: Making 1 variable from 4 variables

PG, thank you. IT WORKS for frequencies.  But how to do I get them to print?  For example:

Each person has quart 1  quart 2 quart 3 quart 4.  I want them printed out that way. So I want to print them as:

id quart1score quart2score quart3score quart4score

 

(not exactly those varnames)

 

 

 

data want;

data old;

proc print;

var id quart ?  quart ?   

run;

 

I will probably think of it later. But answer just in case. You are so HELPFUL. 

I prob. need to format them. they are numbers like 68  58.

 

 

Marysia. Thanks again. 

 

Esteemed Advisor
Posts: 5,521

Re: Making 1 variable from 4 variables

Simple way to print the means with proc tabulate:

 

proc tabulate data=want format=5.1;
class id quart;
var quartMean;
table id, quart*mean=""*quartMean="";
run;
PG
Contributor
Posts: 23

Re: Making 1 variable from 4 variables

PG,

 

Your program did work. 

 

Thank you very much. 

 

However, instead of the tabulate feature, how do make it as this type of variable?  

I need 1 variable with 4 options.  I need to do a repeated ANOVA to determine if the scores differ by sex.  Or perhaps, with the kindness of your heart,  you could tell me how to do that with the 4 variables.  

I thank you!!!

 

data hope;

set old (DONT KNOW WHICH ONE, YOURS?);

proc format library=library2;

value scoreave 1='sim1-5'     2='sim6-10'   3='sim11-15'     4='sim16-sim20';

run;

 

data hope2;

set hope;

if meanscore=1 then scoreave=1;

else if meanscore=2 then scoreave=2;

.

.

format scoreave scoreave.;

run;

Respected Advisor
Posts: 4,736

Re: Making 1 variable from 4 variables

@mgrzyb

If you really want people to motivate to post working and tested code then you need to post a working and tested SAS data step which creates representative sample data and then also show us in detail how the desired result should look like.

Contributor
Posts: 23

Re: Making 1 variable from 4 variables

Hi All,

 

I solved my problem.  I wanted to do a repeated analyses.  

 

First, this, as someone kindly helped me, provided a tabulate function to make them one. The code PG gave me is:

 

 

/* This works for tabulate*/
/*data a;
set elina.mg2;
quart = 1; quartMean = mean(of s1-s5); output;
quart = 2; quartMean = mean(of s6-s10); output;
quart = 3; quartMean = mean(of s11-s15); output;
quart = 4; quartMean = mean(of s16-s20); output;

run;

 

proc tabulate data=a format=5.1;
class id quart;
var quartMean;
table id, quart*mean=""*quartMean="";
run;

 

However, b/c I needed it for repeated ANOVA and MANCOVA procs, I did this: 

data a; 
set olddata;
time1=(sum(s1,s2,s3,s4,s5)/5);   /* the sum fx worked as well */
time2=(sum(s6,s7,s8,s9,s10)/5);
time3=(sum(s11,s12,s13,s14,s15)/5);
time4=(sum(s16,s17,s18,s19,s20)/5);
run;


proc glm data=mydataset;
class gamermg;  /*this is a yes and no variable*/
model time1 time2 time3 time4 = gamermg;
repeated time 4 ;
lsmeans gamermg / out=means;
run;
quit;
proc print data=means;
run;

goptions reset=all;
symbol1 c=blue v=star h=.8 i=j;
symbol2 c=red v=dot h=.8 i=j;
symbol3 c=green v=square h=.8 i=j;
symbol4 c=cyan v=plus h-.8 i-j;
axis1 order=(10 to 100 by 10) label=(a=90 'Means'); /*angle =a*/
axis2 label=('Time') value=('1' '2' '3' '4');
proc gplot data=means;
plot lsmean*_name_=gamermg / vaxis=axis1 haxis=axis2;
run;
quit;

 

SO thank YOU for all YOUR HELP!!!!

 

 

Super User
Posts: 23,683

Re: Making 1 variable from 4 variables

You should use the MEAN() function instead of SUM() for your data step.

Contributor
Posts: 23

Re: Making 1 variable from 4 variables

Thanks.  I actually did use the mean function.  I did it two ways to make sure all was ok.  I copied the wrong one. Thanks though!!

 

Marysia

Ask a Question
Discussion stats
  • 11 replies
  • 167 views
  • 0 likes
  • 5 in conversation