Standard Deviaiton

Accepted Solution Solved
Reply
Contributor
Posts: 25
Accepted Solution

Standard Deviaiton

Hi all, I am trying to calculate monthly standard deviation of returns dataset. I have tried the following:

 

proc sort data=have;
by NCUSIP date year month;
run;

---
proc sql;
create table want as
select *, std(RET) as Result
from have
group by NCUSIP,date,year,month;
quit;

 

For some reason, this does not give me the result. Can someone kindly suggest what to do?


Accepted Solutions
Solution
‎12-21-2016 07:06 AM
Super User
Posts: 9,682

Re: Standard Deviaiton



proc sql;
create table want as
select  NCUSIP,year,month, std(RET) as Result
from have
group by NCUSIP,year,month;
quit;

View solution in original post


All Replies
Super User
Super User
Posts: 7,405

Re: Standard Deviaiton

Sorry, what does "this does not give me the result" mean?  Does it not give you a dataset, is the value missing, is the value not what you were expecting, are there errors/warnings etc.  Post test data - in the form of a datastep, and what the output should like.  Also post the log if there are error/warnings (as text in the post).

Contributor
Posts: 25

Re: Standard Deviaiton

Actually, the log does not give any error. I noticed that it doesn't calculate monthly SD as I wanted when I picked a random month and calculated the standard deviation. Further, the output sort the data by NCUSIP rather than date which I wanted. Was I able to explain clearly?
Super User
Super User
Posts: 7,405

Re: Standard Deviaiton

[ Edited ]

Edit, scrub that.  @Ksharp might have it.  I had assumed you knew what you were doing with the *.  select * means select all variables in the dataset - which will affect the summary statistic.  It is never a good idea to use select *, its just lazy programming.

 

 

 

Sorry, again, I don't know "calculate monthly SD as I wanted" what you wanted?  The std() function returns per the documentation?

http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000245951.htm

 

Perhaps your looking for something different, but there are no examples given to advise.  What does a proc means give you from the data:

proc means data=have;
  by ncusip date year month;
  var ret;
  output out=want std=std;
run;

Note consistent casing and indentations!  

 

Solution
‎12-21-2016 07:06 AM
Super User
Posts: 9,682

Re: Standard Deviaiton



proc sql;
create table want as
select  NCUSIP,year,month, std(RET) as Result
from have
group by NCUSIP,year,month;
quit;

Contributor
Posts: 25

Re: Standard Deviaiton

Thank you very much. Your suggestion worked. It seems the only problem with my code is the inclusion of dates in 'group by'. Can you kindly explain why it affected the result?
Super User
Posts: 9,682

Re: Standard Deviaiton

If you want a single value (std) for each group .
You only need include GROUP variable and that summary statistic STD.


Or you could try RW9's proc means code, both yield the same result.

Super User
Posts: 10,500

Re: Standard Deviaiton

"Group by" means exactly that, treat the combination of those variables as a single group and do the summary statistic within that group. When you include Date, assuming is a single day, then you may as well forget Year and Month as the finest level of summary will occur at date level.

 

If you are desiring separate summaries for the Year, then month (likely not a good idea unless you really want Jan 2015 and 2016 combined) then you may want something different. I would tend to look towards proc summary/means with NCUSIP, Year and Date as Class variables but use a Format for date of MMYY for date (assuming it is an actual SAS date value). That by default would generate summaries for the variable across all records (_type_=0)  NCUSIP across all the data, year across all the data (NCUSIPs) and MMYY across all the NCUSIP, and then combinations of them, the _type_ variable indicating which combinations. You could generate a CLASSDATA set to specify which combinations of interest you want.

It may be that you actually only want NCUSIP and Date with the appropriate format.

☑ This topic is SOLVED.

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

Discussion stats
  • 7 replies
  • 215 views
  • 1 like
  • 4 in conversation