SAS color series separately in SGPLOT - sas

Let's say I have four X-Y plots that I want to plot on the same figure. How can I color each line plot separately?
I've tried the below code:
proc sgplot data = band;
*styleattrs datacolors=(lightblue red green blue black purple brown yellow);
series x = X y = a /markerattrs=(color=green);
series x = X y = b /markerattrs=(color=blue);
series x = X y =c /markerattrs=(color=lightblue);
series x = X y =d /markerattrs=(color=red);
run;
Cheers.

you almost did it
proc sgplot data = band;
series x = X y = a /lineattrs=(color=green);
series x = X y = b /lineattrs=(color=blue);
series x = X y =c /lineattrs=(color=lightblue);
series x = X y =d /lineattrs=(color=red);
run;
By the way, datacolors= only take effect on different group, not different plot.

Related

How to calculate performance curve for each row of data

I want to plot a performance curve for each row of data I have.
A simple version of what I want to do is plot the function with the equation as Y= m*X+b, where I have a table with m and b values and I want Y values for X = 1 to 10.
How is this calculated?
A Y = mX + b example can be seen in the following plot:
The following works:
WITH NUMBERS AS
(
SELECT N FROM (VALUES (1),(2),(3),(4),(5),(6),(7),(8),(9),(10))N(N)
),
Examples AS
(
SELECT m,b FROM (VALUES (1,2),(2,2))N(m,b)
)
SELECT
'Y = ' + CAST(Examples.m as varchar(10)) + 'X + ' + CAST(Examples.b as varchar(10)) AS Formula
,Numbers.N AS X
, Numbers.N * Examples.m + Examples.b
FROM Examples
CROSS JOIN NUMBERS

Piecewise linear regression with SAS PHREG

How to implement a piecewise linear regression model in PHREG procedure of SAS?
For example with one knot at X=T:
Y = β_10 + β_11 . X if X ≤ T
Y = β_20 + β_21 . X if X >T
Given the model with the constraint of continuity:
Y = β_10 + β_11 . X if X ≤ T
Y = β_10 + (β_11 - β_21) T + β_21 . X if X >T
i.e :
Y= β_0 + β_1 . X + S_1
where
S_1 = ( β_11 - β_21 ) T if X >T and 0 otherwise.
Finally i would like to include it in a Cox model:
Proc PHREG
Model time * cas (censure) = X S_1 ;
Run ;
But the problem is S_1 has unknown beta coefficients in it.
Thanks for your help!

Error:'no variables defined' in stata when using monte carlo simulation

I have written the program below and keep getting the error message that my variables are not defined.
Can somebody plese see where the error is and how I should adapt the code? Really nothing seems to work.
program define myreg, rclass
drop all
set obs 200
gen x= 2*uniform()
gen z = rnormal(0,1)
gen e = (invnorm(uniform()))^2
e=e-r(mean)
replace e=e-r(mean)
more
gen y = 1 + 1*x +1*z + 1*e
reg y x z
e=e-r(mean)
replace e=e-r(mean)
more
gen y = 1 + 1*x +1*z + 1*e
reg y x z
more
return scalar b0 =_[_cons]
return scalar b1=_[x]
return scalar b2 =_[z]
more
end
simulate b_0 = r(b0) b_1 = r(b1) b_2 = r(b2), rep(1000): myreg
*A possible solution with eclass
capture program drop myreg
program define myreg, eclass
* create an empty data by dropping all variables
drop _all
set obs 200
gen x= 2*uniform()
gen z = rnormal(0,1)
gen e = (invnorm(uniform()))^2
qui sum e /*to get r(mean) you need to run sum first*/
replace e=e-r(mean)
gen y = 1 + 1*x +1*z + 1*e
reg y x z
end
*gather the coefficients (_b) and standard errors (_se) from the *regression each time
simulate _b _se, reps(1000) seed (123): myreg
* show the final result
mat list r(table)
* A possible solution with rclass
* To understand the difference between rclass and eclass, see the Stata manual(http://www.stata.com/manuals13/rstoredresults.pdf)
capture program drop myreg
program define myreg, rclass
drop _all
set obs 200
gen x= 2*uniform()
gen z = rnormal(0,1)
gen e = (invnorm(uniform()))^2
qui sum e
replace e=e-r(mean)
gen y = 1 + 1*x +1*z + 1*e
reg y x z
mat output=e(b)
return scalar b0=output[1,3]
return scalar b1=output[1,1]
return scalar b2=output[1,2]
end
simulate b_0=r(b0) b_1=r(b1) b_2=r(b2), rep(1000) seed (123): myreg
return list
*P.S. You should read all the comments as suggested by #Nick to fully understand what I did here. .

SAS - Labeling Kernel Density Line Intersections in SGplot with multiple histograms

Disclaimer: I am an amateur programmer. I am a student.
So I've been working on a Van Westendorf price model which requires analysis of critical points produced when the kernel density lines overlayed on 4 histograms intersect. My current model has 3 histograms, so 3 lines, and 2 intersections. How can I label these intersections?
You can see the output here.
http://i.imgur.com/6TGWv4l.jpg
Here is the code:
Proc Import Out = work.SASdata datafile =
"C:\Users\jdelizza\Desktop\SimpleSAS.xls"
DBMS = xls;
Sheet = "Asthma";
Getnames = yes;
Label I_Price_A = 'Too Expensive'
I_Price_B = 'Inexpensive'
I_Price_C = 'Slightly Expensive'
F_Price_Low = 'Full Price Inexpensive'
F_Price_Expensive = 'Full price Expensive'
F_Price_Too_Expensive = 'Full Price Too Expensive'
P_Price_Low = 'Personalized Price Inexpensive'
P_Price_Expensive = 'Personalized price Expensive'
P_Price_Too_Expensive= 'Personalized Price Too Expensive';
run;
proc sgplot;
Title "Those with Asthma Indoor Pricing";
histogram I_Price_A / fillattrs=graphdata1 transparency = .5 binstart =1
binwidth=50;
density I_Price_A /type = kernel lineattrs=graphdata1;
histogram I_Price_B / fillattrs=graphdata2 transparency=.5 binstart=1
binwidth=50;
density I_Price_B /type = kernel lineattrs=graphdata2;
histogram I_Price_C / fillattrs=graphdata3 transparency=.5 binstart=1
binwidth=50;
density I_Price_C / type = kernel lineattrs=graphdata3;
keylegend / location=inside position=topright noborder across=2;
yaxis grid;
xaxis display=(nolabel) values=(0 to 1000 by 100);
run;

Why SAS string concat cannot involve the variable itself?

This issue drives me crazy. In SAS, when I want to concat a string, the variable which will be assigned to the result cannot be used in the input.
DATA test1;
LENGTH x $20;
x = "a";
x = x || "b";
RUN;
Result: x = "a";
DATA test2;
LENGTH x $20;
y = "a";
x = y || "b";
RUN;
Result: x = "ab";
DATA test3;
LENGTH x $20;
x = "a";
y = x;
x = y || "b";
RUN;
Result: x = "a";
The last one is so strange. x is not even involved in concat directly.
This does not make sense. Because 1) you can do other operations in this way, e.g. transtrn, substr. 2) SAS does not give any warning message.
Why?
It's because the length of X is initially set 20, so it has 19 trailing blanks. If you add b, there isn't room for it, because of the trailing blanks. Either trim the x before cat operator or use catt. You can use lengthc to see the length of the character variable.
DATA test1;
LENGTH x $20;
x = "a";
len=lengthc(x);
x = trim(x) || "b";
*x = catt(x, b);
RUN;
proc print data=test1;
run;
You could also use substr() on the left hand side of the equation. Something like:
substr(x,10,1) = 'a';
to set the 10th car to 'a'. Then loop over each character in x (where the 10 is).