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

Hello,

 

I have a difficulty to get variables' value from other two variables. I have three continuous variables: x1, x2, x3. 

x2=mean(x1)*(1-x1**2);

x3=(mean(x1)+mean(x2))*(1-x2**2).

 

Here mean(x1) is the mean of all x1 values, the same as mean(x2). If I use the formula directly in data step, then mean(x1) will equal the current value of x1, not the mean of all x1 values. Is there one can help me figure out how to get the correct values of x2 and x3? Thanks a lot!

 

Rosie 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

@RosieSAS wrote:
Sorry that I didn't make my question clear. At first I only have the values of x1, then using x1 to get x2, then using x1 and x2 to get x3.

So there is no way to avoid making multiple passes through the data to do that.

 

PROC SQL syntax is easier to specify since you can use nested sub queries.

proc sql ;
create table want as
select step2.x1
     , step2.x2
     , (step2.x1_mean+mean(step2.x2)*(1-step2.x2**2) as x3
 from (
  select step1.x1
       , step1.x1_mean
       , x1_mean*(1-step1.x1**2) as x2 
   from (
        select have.x1,mean(have.x1) as x1_mean from have
        ) step1
      ) step2
;
quit;

You probably don't need to use all of those aliases (step1,step2) in the variable references, but I just spelled it out in detail so you can see the sequential nature of the steps. 

View solution in original post

5 REPLIES 5
BigD
Calcite | Level 5
I think you will need to calculate the mean values first, then merge them back to the original data, and then do your calculations.
RosieSAS
Obsidian | Level 7
Thanks! I'm wondering whether there is a quicker way? This way needs to use two PROC MEANS and two merges.
Tom
Super User Tom
Super User

It looks strange to change the value of X2 and then reference the MEAN of X2.  Do you want the mean from the original value?  Or the mean of the modified value?

 

But in general you will need to calculate the means first.  Then you can use them.

For example:

proc summary data=have ;
  var x1 x2 ;
  output out=means mean= /autoname;
run;
data want;
  set have;
  if _n_=1 then set means;
  x2=x1_mean*(1-x1**2);
  x3=(x1_mean+x2_mean)*(1-x2**2);
run;

PROC SQL makes that easier as it will automatically remerge aggregate values onto the detailed observations for you.

proc sql;
create table want as 
  select *
       , mean(x1)*(1-x1**2) as new_x2
       , (mean(x1)+mean(x2))*(1-x2**2) as new_x3
  from have
;
quit;

 Note that the MEAN() function in PROC SQL is the aggregate function.  If you want to use the SAS function in PROC SQL code you must specify at least two arguments so the compiler/parser can tell which one you want.

RosieSAS
Obsidian | Level 7
Sorry that I didn't make my question clear. At first I only have the values of x1, then using x1 to get x2, then using x1 and x2 to get x3.
Tom
Super User Tom
Super User

@RosieSAS wrote:
Sorry that I didn't make my question clear. At first I only have the values of x1, then using x1 to get x2, then using x1 and x2 to get x3.

So there is no way to avoid making multiple passes through the data to do that.

 

PROC SQL syntax is easier to specify since you can use nested sub queries.

proc sql ;
create table want as
select step2.x1
     , step2.x2
     , (step2.x1_mean+mean(step2.x2)*(1-step2.x2**2) as x3
 from (
  select step1.x1
       , step1.x1_mean
       , x1_mean*(1-step1.x1**2) as x2 
   from (
        select have.x1,mean(have.x1) as x1_mean from have
        ) step1
      ) step2
;
quit;

You probably don't need to use all of those aliases (step1,step2) in the variable references, but I just spelled it out in detail so you can see the sequential nature of the steps. 

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 674 views
  • 0 likes
  • 3 in conversation