Using correlate in Stata to compare within a variable I am trying to compare correlations between subgroups of a variable. For instance, I am trying to compare the income of group 1 v. income of group 2 v. income of group 3 v. income of group 4, where the groups are specified by another categorical variable. 
What I did was create new variables separating out the income variable into four new variables (i.e. income_1, income_2, income_3, income_4). Naturally, there are missing values that look something like this (each bracketed portion represents a new line where the '.' represents missing values):   
[income_1   income_2    income_3   income_4]  
[1 . . . ]  
[1 . . .]  
[. 2 . .]  
[. 2 . .]  
[. . 1 .]  
[. . 1 .]  
[. . . 2]  
[. . . 2]  

So when I try to perform correlate on these four values, I get an error that says 'no observations' probably because correlate performs listwise or pairwise deletion. 
How can I fix this problem? I don't know how to get rid of the missing values.
 A: As @Stask says in a comment, it doesn't really make sense to calculate a correlation in this case. Unless this is some kind of time-series/panel data, in which case the answer might be to do a reshape wide followed by a xpose or something else clever like that.
However, given that you want to do what you've described...
use http://www.ats.ucla.edu/stat/stata/notes/hsb2, clear

then:
gen write1=.
gen write2=.
gen write3 =.
replace write1=write if  write>30 & write <=40
replace write2=write if  write>40 & write <=50
replace write3=write if  write>50

putmata write1, omitmissing       
getmata write1, replace force

putmata write2, omitmissing       
getmata write2, replace force

putmata write3, omitmissing       
getmata write3, replace force

list write1 write2 write3 in 1/10

         +--------------------------+
         | write1   write2   write3 |
         |--------------------------|
      1. |     33       44       52 |
      2. |     39       46       59 |
      3. |     40       46       52 |
      4. |     37       49       52 |
      5. |     38       49       59 |
         |--------------------------|
      6. |     31       44       57 |
      7. |     31       44       55 |
      8. |     31       41       65 |
      9. |     40       47       60 |
     10. |     33       41       63 |
         +--------------------------+

cor  write1 write2 write3

                 |   write1   write2   write3
    -------------+---------------------------
          write1 |   1.0000
          write2 |   0.2635   1.0000
          write3 |   0.0393  -0.3456   1.0000

Not sure if that's what you want or not. Write1 only has 24 elements, while write3 has 126, and it appears that corr will thus only use the first 24 elements of each list in its calculations.
Obviously, you've also mangled write1, write2, and write3 so that they are in the wrong observations, so you'd want to drop write1 write2 write3 when you're done.
Perhaps you could do the corr strictly in Mata and avoid some messiness. I just got Stata a few days ago, so really don't know Mata yet.
A: Have you tried the by-functionality?
  sysuse auto, clear
  bysort foreign : corr mpg weight

A: use http://www.ats.ucla.edu/stat/stata/notes/hsb2, clear

I will find the correlation of write for three different groups generated as follows:
gen write1=.
gen write2=.
gen write3 =.
replace write1=write if  write>30 & write <=40
replace write2=write if  write>40 & write <=50
replace write3=write if  write>50

list write1 write2 write3 in 1/10

  list write1 write2 write3 in 1/10

     +--------------------------+
     | write1   write2   write3 |
     |--------------------------|
  1. |      .        .       52 |
  2. |      .        .       59 |
  3. |     33        .        . |
  4. |      .       44        . |
  5. |      .        .       52 |
     |--------------------------|
  6. |      .        .       52 |
  7. |      .        .       59 |
  8. |      .       46        . |
  9. |      .        .       57 |
 10. |      .        .       55 |
     +--------------------------+

cor  write1 write2 write3
no observations
r(2000);

Yes, there are missing values so you can't compute correlation. Now instead of missing I assign 0 for all which doesn't satisfy the given if condition. 
gen write1=0
gen write2=0
gen write3 =0
replace write1=write if  write>30 & write <=40
replace write2=write if  write>40 & write <=50
replace write3=write if  write>50
list write1 write2 write3 in 1/10

     +--------------------------+
     | write1   write2   write3 |
     |--------------------------|
  1. |     31        0        0 |
  2. |     36        0        0 |
  3. |     39        0        0 |
  4. |     39        0        0 |
  5. |     38        0        0 |
     |--------------------------|
  6. |     33        0        0 |
  7. |     35        0        0 |
  8. |     37        0        0 |
  9. |     33        0        0 |
 10. |     31        0        0 |

cor  write1 write2 write3
(obs=200)

             |   write1   write2   write3
-------------+---------------------------
      write1 |   1.0000
      write2 |  -0.2117   1.0000
      write3 |  -0.4760  -0.7454   1.0000

