How to create a function that gets user defined values (strings, numbers etc.) as input and user defined values (strings,numbers etc.) as output. Have searched online and it seems surprisingly complicated. Still, there are user-made packages in stata that have (python or R like) custom functions so this should be possible within Stata as well.
Here is a MWE in python but would like to have the same in Stata. The `paste_text`function should suffice to get the idea, but for completeness I've also added the `paste_text_with_conditions` as that is what I ultimately want to achieve this time.
Then later I would like to use it in, for example the following way
Here is a MWE in python but would like to have the same in Stata. The `paste_text`function should suffice to get the idea, but for completeness I've also added the `paste_text_with_conditions` as that is what I ultimately want to achieve this time.
Code:
def paste_text(x:str,y:str): return f"I {x} want to output a string based on some parameters, {y}!" print(paste_text("really","thank you")) # output # I really want to output a string based on some parameters, thank you! def paste_text_with_conditions(x:str,z:int): if z >=1: string= f"I {x} want to output a string based on some parameters!" else: string= f"It {x} should not be that difficult" return string print(paste_text_with_conditions("really",0)) # output # I really want to output a string based on some parameters, thank you! #It really should not be that difficult
Code:
use paste_text_with_conditions("text",1), clear
Comment