DATA Step, Macro, Functions and more

How to create a new variable from values of previous variables that meet certain conditions

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 13
Accepted Solution

How to create a new variable from values of previous variables that meet certain conditions

I am trying to create a new variable by dividing the values of single variable "SNinner_SNouter" where another variable has a values of '8' by that same variable "SNinner_SNouter" where that same other variable has a value of 19. 

 

I feel like i need some statement like    new var = "SNinner_SNouter" where value is 8    /    "SNinner_SNouter" where value is 19

 

 

I have attached the dataset. I appologise that my explanation is probably terrible. I'm not experienced at programming. 

 

 


Accepted Solutions
Solution
a week ago
Respected Advisor
Posts: 3,019

Re: How to create a new variable from values of previous variables that meet certain conditions

[ Edited ]

You can use the LAG function, or you can simply re-arrange the data as follows. Both assume the data is properly sorted.

 

data want;
    merge ratios(where=(global_timepoint=8) rename=(SNinner_SNouter=SN8))
        ratios(where=(global_timepoint=19) rename=(SNinner_SNouter=SN19));
    by date_of_experiment;
    ratio = SN19 / SN8;
run;

 

--
Paige Miller

View solution in original post


All Replies
Super User
Super User
Posts: 8,111

Re: How to create a new variable from values of previous variables that meet certain conditions


@Gjax21 wrote:

I am trying to create a new variable by dividing the values of single variable "SNinner_SNouter" where another variable has a values of '8' by that same variable "SNinner_SNouter" where that same other variable has a value of 19. 

 

I feel like i need some statement like    new var = "SNinner_SNouter" where value is 8    /    "SNinner_SNouter" where value is 19

 

 

I have attached the dataset. I appologise that my explanation is probably terrible. I'm not experienced at programming.  

 


You need to explain more clearly what you want. 

Do the values you need to work with already appear on the same observation (or ROW if you are looking at your spreadsheet)?

If not how do you know which observations to combine so that the values ARE on the same observation?  Is there an id variable that would let you merge the observations together? 

 

Do you want to compare to the value on the previous observation?  Again what are the id variables and is the data already sorted?

 

Please post small sample data with minimal set of variables and observations to demonstrate the issue.  Please post in the form of a data step that can be used to create the dataset.  Not a copy of a spreadsheet.

Occasional Contributor
Posts: 13

Re: How to create a new variable from values of previous variables that meet certain conditions

[ Edited ]

 

data ratios;

input Date_of_Experiment$ Global_Timepoint    SNinner_SNouter ;     

cards;

2_15 8 0.99789
2_15 19 1.00253
2_18 8 1.07797
2_18 19 1.10799
2_20 8 0.9677
2_20 19 0.9748
2_27 8 1.08788
2_27 19 1.08133
;;;;;run;quit;

 

 

The experimental case is indicated by Date_of_Experiment. Global timepoint indicates a numerical designation of a point in time that corresponds to an experimental change. Within each experimental case indicated by "Date_of_Experiment" I want to divide the value of SNinner_SNouter  at timepoint 19 by the value at timepoint 8 and write the result as a new variable for each experimental case. As you can see the problem is that they are in different rows. 

Solution
a week ago
Respected Advisor
Posts: 3,019

Re: How to create a new variable from values of previous variables that meet certain conditions

[ Edited ]

You can use the LAG function, or you can simply re-arrange the data as follows. Both assume the data is properly sorted.

 

data want;
    merge ratios(where=(global_timepoint=8) rename=(SNinner_SNouter=SN8))
        ratios(where=(global_timepoint=19) rename=(SNinner_SNouter=SN19));
    by date_of_experiment;
    ratio = SN19 / SN8;
run;

 

--
Paige Miller
Occasional Contributor
Posts: 13

Re: How to create a new variable from values of previous variables that meet certain conditions

Posted in reply to PaigeMiller

Thanks very much Paige! 

 

This worked very well once I sorted the data. I did not have much luck trying to figure out how to do it by googling. Quick and efficient. 

☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 106 views
  • 0 likes
  • 3 in conversation