Thank you Tom for your quick response! I may have used lag() incorrectly. This is the first time I am using it. I thought it will sort of retain the value of the previous observation in the current observation to do some analysis. I will have to read more on lag(). lets start fresh... Obs name col2 col3 id
1 C Y,C,Z C 2
2 Y Y,H,J Y 1
3 Z H,K,R 3 from the above dataset I want to create a new variable 'new_var' that will have the value of 'col_3'... if: 1) id = 1 then new_var = y 2) say id=1 's respective col3 value is blank, then only then, go to the next id value that is (2). if it has a respective observation in col3 then new_var = c. 3) say id= 1 and id =2 's corresponding col3 values are blank, then and then only go to id=3....and so on how will I do this for the above dataset...If I were to sort I would sort by the dataset by id. Can you please help me with the logic to do that? I hope this clears all the confusion. Note: This is a small part of the entire program and the id is dynamic for a dataset. I will have many such datasets like above with different id values( meaning ds-1 will have id's 1,2...DS-2 will have ids 1,2,3, ds-3 will have ids 1 etc..). but the logic is the same for all the datasets... Thank you
... View more