I apologize in advance if this question is silly, but how do I create a mean variable by groups. Take the below table for example, I want to calculate a Var2 such that for the first 3 observations, Var2 is the mean(Var1 of the first 3 obs), and Var2 for the next 7 obs is mean(Var1 from id=4 to id=10). ie, to get from Table 1 to Table 2. Thank you! Table 1 ID Group Var1 1 A 1 2 A 1 3 A 4 4 B 3 5 B 3 6 B 3 7 B 3 8 B 3 9 B 3 10 B 10 Table 2 ID Group Var1 Var2 1 A 1 2 2 A 1 2 3 A 4 2 4 B 3 4 5 B 3 4 6 B 3 4 7 B 3 4 8 B 3 4 9 B 3 4 10 B 10 4
... View more
Considering the following 7 tables, I currently have test.set1, and test.port199001 to test.port199005 (199001 simply refers to all the data from January of 1990, etc). I want to arrive at test.set2. So strictly speaking, I want to fill Var2 of test.set1 with the values from each of the individual test.portXXXXXX based the matching ID and date. How would I be able to do that?? Thank you very much in advance for helping me out! test.set1 ID Date Var1 Var2 1 199001 0.424477 . 2 199002 0.919131 . 3 199003 0.188546 . 4 199004 0.294916 . 5 199005 0.589303 . test.port199001 ID Var2 1 0.60478 2 0.696379 3 0.890347 4 0.281266 5 0.682892 test.port199002 ID Var2 1 0.1342 2 0.112347 3 0.129106 4 0.354384 5 0.290145 test.port199003 ID Var2 1 0.776273 2 0.156324 3 0.981456 4 0.006446 5 0.98249 test.port199004 ID Var2 1 0.524929 2 0.037742 3 0.633113 4 0.548542 5 0.324782 test.port199005 ID Var2 1 0.591753 2 0.507835 3 0.078124 4 0.244329 5 0.230047 test.set2 ID Date Var1 Var2 1 199001 0.424477 0.60478 2 199002 0.919131 0.112347 3 199003 0.188546 0.981456 4 199004 0.294916 0.548542 5 199005 0.589303 0.230047
... View more
Thanks! Both seems to work great! But a silly question (don't think it would be worth starting a new post), how would I create a variable that is equal to the maximum number of observations in the dataset (so actually this variable would be the same for every observation)? Leon
... View more
there are 11, and they are permno, hexcd, date, ret, prc, shrout, gvkey, linkdt, linkenddt, bkvlps. And it is by the variable "date" that I watn to break into into monthly parititions. The data spans from 1990 to 2010, so effectively into 252 smaller tables. Thanks! Leon
... View more
I've read the post, but I am new to SAS; would you be able to explain what the hash does? data _null_ ; dcl hash hh ( ) ; hh.definekey ('k' ) ; hh.definedata ('sex', 'name', 'age', 'height', 'weight') ; hh.definedone () ; do until(mod(k,5)=0 or last); k+1; set sashelp.class end=last ; hh.add(); end; gp+1; hh.output(dataset: 'a'||strip(gp)); run; in particularly dcl has hh, hh.define..., and mod. Thank you!
... View more
I have a dataset which goes from 1990 to 2010; and for each year, there are few hundred observations. And I am trying to "partition" the dataset into different tables by year. I have the following, but I am pretty sure I can't do it, so what is the correct way to do it? data work.test2; do y=1989 to 2010; set work.test; if year(date)=y; end; run; Thank you very much!
... View more