BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
npr
Fluorite | Level 6 npr
Fluorite | Level 6

I have the below table 

 

DateXYZ
2001Q3152482.897195.0332
2001Q4153403.273197.4615
2002Q1154303.825499.3809
2002Q2155213.9667100.9855
2002Q3156134.0845104.8513
2002Q4157054.4269107.0016
2003Q1157953.3352108.6860
2003Q2158862.8685110.5227
2003Q3159783.4837113.8978
2003Q4160703.8459115.8557
2004Q1161614.1233120.9971
2004Q2162523.7870122.2159
2004Q3163443.5027124.7153
2004Q4164363.2087128.2267

 

I calculate the std deviation of y and z till from 2001 q4 to  2003 q2 . 

 

I have to create a new table which should look like below 

 

DateXYZ
2001Q3152482.897195.0332
2001Q4153403.273197.4615
2002Q1154303.825499.3809
2002Q2155213.9667100.9855
2002Q3156134.0845104.8513
2002Q4157054.4269107.0016
2003Q1157953.3352108.6860
2003Q2158862.8685110.5227
2003Q3159783.4837 + Calculated std deviation of y 113.8978 - calculated std deviation of Z
2003Q4160703.8458++ Calculated std deviation of y 115.8556 - calculated std deviation of Z
2004Q1161614.1232+ Calculated std deviation of y 120.9971 - calculated std deviation of Z
2004Q2162523.7869+ Calculated std deviation of y 122.2158 - calculated std deviation of Z
2004Q3163443.5026+ Calculated std deviation of y 124.7152 - calculated std deviation of Z
2004Q4164363.2086+ Calculated std deviation of y 128.2266 - calculated std deviation of Z

 

I tried using the case statement inside proc sql, but it did not work. Any assistance will be appreciated. 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

@npr wrote:
i only want the standard deviation of columns y and z , from 2001 q3 to 2003 q2

That's what my code does. It adds/subtracts the result of summary function std for all periods before 2003q3 to all periods starting with 2003q3.

View solution in original post

6 REPLIES 6
PaigeMiller
Diamond | Level 26

@npr wrote:

I tried using the case statement inside proc sql, but it did not work. Any assistance will be appreciated. 


Show us the code you tried. Explain what "did not work".

--
Paige Miller
npr
Fluorite | Level 6 npr
Fluorite | Level 6
create table tablenew as
select
Date,
X,
case when Date >= "2003Q3" then (y+std(y)) else y end as y,
case when Date >= "2003Q3" then (z-std(z)) else z end as z
from have
;
quit;
Kurt_Bremser
Super User

You need to restrict the time range for the calculation of std:

data have;
input date :yyq6. x y z;
format date yyq6.;
datalines;
2001Q3 15248 2.8971 95.0332
2001Q4 15340 3.2731 97.4615
2002Q1 15430 3.8254 99.3809
2002Q2 15521 3.9667 100.9855
2002Q3 15613 4.0845 104.8513
2002Q4 15705 4.4269 107.0016
2003Q1 15795 3.3352 108.6860
2003Q2 15886 2.8685 110.5227
2003Q3 15978 3.4837 113.8978
2003Q4 16070 3.8459 115.8557
2004Q1 16161 4.1233 120.9971
2004Q2 16252 3.7870 122.2159
2004Q3 16344 3.5027 124.7153
2004Q4 16436 3.2087 128.2267
;

proc sql;
create table tablenew as
select
  Date,
  X,
  case
    when Date >= input("2003Q3",yyq6.)
    then y + (select std(y) from have where date lt input("2003Q3",yyq6.))
    else y
  end as y,
  case
    when Date >= input("2003Q3",yyq6.)
    then z - (select std(z) from have where date lt input("2003Q3",yyq6.))
    else z
  end as z
from have
;
quit;
npr
Fluorite | Level 6 npr
Fluorite | Level 6
i only want the standard deviation of columns y and z , from 2001 q3 to 2003 q2
Kurt_Bremser
Super User

@npr wrote:
i only want the standard deviation of columns y and z , from 2001 q3 to 2003 q2

That's what my code does. It adds/subtracts the result of summary function std for all periods before 2003q3 to all periods starting with 2003q3.

Kurt_Bremser
Super User

Please post data in readily usable form, like

data have;
input date :yyq6. x y z;
format date yyq7.;
datalines;
2001Q3 15248 2.8971 95.0332
2001Q4 15340 3.2731 97.4615
2002Q1 15430 3.8254 99.3809
2002Q2 15521 3.9667 100.9855
2002Q3 15613 4.0845 104.8513
2002Q4 15705 4.4269 107.0016
2003Q1 15795 3.3352 108.6860
2003Q2 15886 2.8685 110.5227
2003Q3 15978 3.4837 113.8978
2003Q4 16070 3.8459 115.8557
2004Q1 16161 4.1233 120.9971
2004Q2 16252 3.7870 122.2159
2004Q3 16344 3.5027 124.7153
2004Q4 16436 3.2087 128.2267
;

and show the code you used for calculating the standard dev.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 6 replies
  • 533 views
  • 0 likes
  • 3 in conversation