I have been trying to make a Vector plot in SAS using the SAShelp library 'Class'. The goal is to make a vector plot with the origins of the vectors in the respective means of the x and y variables. I used the following code:
DATA class2;
SET Sashelp.class;
x = mean(height);
y=mean(weight);
run;
PROC SGPLOT DATA = class2;
VECTOR X = height Y = weight / XORIGIN = x YORIGIN = y;
run;
I thus first created two new variables, containing the means of the X and Y variable. Then I wanted to place the origin of the vectors in these origins, but when I do this, the vectors disappear in my plot and all I get is a plot with some dots.
Any suggestions on how to solve this, and especially, how this disappearance of the vectors has been caused?
Thank you in advance,
Koen
The mean() function does not work across the entire data set. Data Steps deal with a single record at a time (think of it as SAS writing an outer loop for processing for you.
You can use SQL to get the means and then add them to the data set. I also added data labels for you.
proc sql noprint;
select mean(height), mean(weight)
into :mh, :mw
from class2;
quit;
DATA class2;
SET Sashelp.class;
x = &mh;
y= &mw;
run;
PROC SGPLOT DATA = class2;
VECTOR X = height Y = weight / XORIGIN = x YORIGIN = y datalabel=name;
run;
Produces this:
Related
I am comparing the evolution of plasma concentrations over time for different treatments of patients.
We applied each treatment to different subjects and for each treatment we want a graph with the evolution for each subject in black, as well as for the the mean in red.
It should look like this
but it does look like this
My data has variable
trtan and trta for treatment number and name
subjid for the patient receiving that treatment
ATPT for timepoint
AVAL for Individual Concentrations
MEAN for average Concentrations
I am using SGPLOT to produce this line plot. y axis has concentrations while x axis has time points, I am sorting data by treatment, subject and timepoint before passing to Proc SGPLOT.
Lines for indivizual subjects are fine, Issue is with mean line plot, Since dataset is sorted by subject i am getting multiple mean plots by subject as well.
My requirement is to have multiple indivizual plots and an overlaying mean plot. Can anyone advise how can i solve this.
I am using below code. How can I repair it?
proc sort data = pc2;
by trtan trta subjid atptn atpt;
run;
proc sgplot data = pc2 dattrmap = anno pad = (bottom = 20%) NOAUTOLEGEND ;
by trtan trta;
series x = atptn y = aval/ group = trta
lineattrs = (color = black thickness = 1 pattern = solid );
series x = atptn y = mean/ group = trta attrid = trtcolor
lineattrs = (thickness = 2 pattern = solid );
xaxis label= "Actual Time (h)"
labelattrs = (size = 10)
values = (0 12 24 36 48 72 96 120 168)
valueattrs = (size = 10)
grid;
yaxis label= "Plasma Concentration (ng/mL)"
labelattrs = (size = 10)
valueattrs = (size = 10)
grid;
run;
This is not a problem with the mean only.
Leave out the mean, ass min=-20 to your yaxis specification, and you will see the same problem.
Alternatively run this code
data pc2;
do subj = 1 to 3;
do time = 1 to 25;
value = 2*sin(time/3) + rand('Normal');
output;
end;
end;
run;
proc sgplot data=pc2;
series x=time y=value;
run;
and you will get
The solution is to have one plot for each subject, so first sort the data by time and transpose it to have one variable subj_1 etc. for each subject.
proc sort data=pc2 out=SORTED;
by time subj;
run;
proc transpose data=TEST out=TRANS prefix=subj_;
by time;
id subj;
run;
I leave it as an exercise for you to add the mean to this dataset.
Then run sgplot with a series statement per subject. To build these statements, we interrogate the meta data in dataset WORK.TRANS
proc sql;
select distinct 'series x=time y='|| name ||'/lineattrs = (color=black)'
into :series_statements separated by ';'
from sasHelp.vColumn
where libname eq 'WORK' and memName eq 'TRANS'
and (name like 'subj%' or name = mean;
quit;
proc sgplot data=TRANS;
&series_statements;
run;
The result, without the mean, looks like this for my example:
Of course, you will have to do some graphical fine tuning.
We can achive it simply by taking the mean by ATPT and then instead of merging the mean record to the PK data by ATPT, you need to append the records and then you can run your code and it will give you the result you are expecting, please let me know if it does not work, it seems to have worked for me.
How do I use the output variable of one PROC into another PROC.
I'm new to SAS and have spend many hours trying to solve this problem in the program below.
DATA FA2;
SET FA2;
proc iml;
start main;
use FA2;
read all var {Close};
s = Close;
u = j(nrow(s)-1,1,0);
do i=2 to nrow(s);
u[i-1]=log(s[i]/s[i-1]);
end;
n=nrow(s)-1;
rsigma=sqrt(252/n*(u'*u));
mu = mean(u);
call qntl(q, u);
print q[rowname={"P05", "P95"}];
s = quartile(u);
PRINT mu, rsigma, s;
finish;
run;
PROC UNIVARIATE DATA = FA2;
var u;
run;
ERROR
PROC UNIVARIATE DATA = FA2;
var u;
Variable U not found.
run;
Your first two lines accomplish nothing and are not useful - remove them.
data fa2;
set fa2;
You are not creating any output data set within the IML statement, and you really shouldn't reuse the same data set name (FA2) so many times. It makes it harder to debug your code. See the example below.
proc iml;
/** create SAS data set from vectors **/
y = {1,0,3,2,0,3}; /** 6 x 1 vector **/
z = {8,7,6,5,6}; /** 5 x 1 vector **/
c = {A A A B B B}; /** 1 x 6 character vector **/
create MyData var {y z c}; /** create data set **/
append; /** write data in vectors **/
close MyData; /** close the data set **/
quit;
IML ends with a QUIT; at the end.
PROC UNIVARIATE is referencing your FA2 data set which does not have U, because you did not save the variable to the data set so it doesn't exist. So it is correctly generating an error. Fixing the above issues should resolve this.
I have a database with serveral variables, including one, RIF, that hase an x^2 shape relative to another variable, Y.
I want to obtain two seperate databases, separated based on whether the observation is on the decreasing or the increasing part of the curve.
I thought I had something by using the lag function, but my code does not work.
proc sort data=have; by y; run;
data want;
set have;
do while (rif<=lag(rif));
Part=1;
end;
if Part ne 1 then Part=2
run;
And the separating given Part, but it seems to create infintite loop.
Is there a mistake in my code / is there a better way of doing this
data have;
do x = -10 to 10 by 1;
y = x**2;
output;
end;
run;
data want;
set have;
lag_y = lag(y);
if _n_ = 1 then Part=.;
else if y <= lag_y then Part=1;
else Part=2;
drop lag_y;
run;
SAS newbie here.
My question is about PROC REG in SAS; let's assume I have already created a model and now I would like to use this model, and known predictor variables to estimate a response value.
Is there a clean and easy way of doing this in SAS? So far I've been manually grabbing the intercept and the coefficients from the output of my model to calculate the response variable but as you can imagine it can get pretty nasty when you have a lot of covariates. Their user's guide is pretty cryptic...
Thanks in advance.
#Reese is correct. Here is some sample code to get you up the learning curve faster:
/*Data to regress*/
data test;
do i=1 to 100;
x1 = rannor(123);
x2 = rannor(123)*2 + 1;
y = 1*x1 + 2*x2 + 4*rannor(123);
output;
end;
run;
/*Data to score*/
data to_score;
_model_ = "Y_on_X";
y = .;
x1 = 1.5;
x2 = -1;
run;
/*Method 1: just put missing values on the input data set and
PROC REG will do it for you*/
data test_2;
set test to_score;
run;
proc reg data=test_2 alpha=.01 outest=est;
Y_on_X: model y = x1 x2;
output out=test2_out(where=(y=.)) p=predicted ucl=UCL_Pred lcl=LCL_Pred;
run;
quit;
proc print data=test2_out;
run;
/*Method 2: Use the coefficients and the to_score data with
PROC SCORE*/
proc score data=to_score score=est out=scored type=parms;
var x1 x2;
run;
proc print data=scored;
var Y_on_X X1 X2;
run;
2 ways:
Append the data you want into the data set you're going to use to get estimates but leave the y value blank. Grab the estimates using the output statement from proc reg.
Use Proc Score
http://support.sas.com/documentation/cdl/en/statug/63347/HTML/default/viewer.htm#statug_score_sect018.htm
I need to find a ratio of two mean values, that I have found using proc means.
proc means data=a;
class X Y;
var x1 x2;
run;
Then I get the output mean values for variables x1 and x2 in the two categories of X and Y, but it is x1/x2 for each category that I am interested in, and doing it by hand is not really a solution.
I am not a professional programmer, so I hope there is a simple piece of code that I can understand and use.
You need to precompute x1/x2 or postcompute x1/x2 (Depending on whether you want mean(x1/x2) or mean(x1)/mean(x2), which can have different answers of x1 and x2 have different numbers of responses).
So either (... means fill in what you have already)
data premean;
set have;
x1x2 = x1/x2;
run;
proc means ... ;
class ... ;
var x1x2;
run;
or
proc means ...;
class ... ;
var x1 x2;
output out=postmeans mean=;
run;
data want;
set postmeans;
x1x2=x1/x2;
run;
proc sql noprint;
create table xy_ratio as /* New table name*/
select distinct X, Y, avg(x1)/avg(x2) as x1_x2_ratio /* selects distinct rows containing variables listed here. (Must include group by variables) mean of x1 / mean of x2 to form ratio*/
from a /*source dataset*/
group by X, Y /*Similar to class statement, will provide an average for each distinct combination of X and Y that appear in the dataset*/
;
quit;