Yes, you can define your own custom variables in QGIS in different levels like as in global Level, as project or layer variables and even as ad hoc variables in QGIS expressions using the function with_variable().
See functions help for with_variable() expression as well as an expample where three different variable are declared: vertex (=20), azimuth1 and azimuth2 (the last two have an expression assigned as values).
Go to the Settings / properties of the desired Level (e.g. right Click layer > properties > tab variables) and select variables to add your own variable by typing a name and the value of the variable. You can then call the variable by its Name preceded with @ - e.g. @my_variable.
See here for more information about custom variables: http://nyalldawson.net/2015/12/exploring-variables-in-qgis-pt-3-layer-level-variables/
The content (value) of the varible can be set to an attribute or a complex expression e.g. to generate all kind of sophisticated data defined styles. So this example: define a project variable called set_color with this value (string):
case
when value = 1 then 'red'
when value = 2 then 'blue'
when value = 3 then 'green'
else 'yellow'
end

Now use the variable @set_color to create a data defined color of a layer, based on its attribute field value using the function eval(): eval(@set_color):

Variables you defined at global, project or layer level will appear in expression string builder dialog under variables:

valuetovalue2and wherever you used this custom variable, settings will use fieldvalue2and notvalueany more. This works even for many different layers at once as long as all of them have a fieldvalue2(orvalue). – Babel Sep 17 '22 at 14:12s-nula to "B"and thens-srov to "D"and altered the expression like thisCASE WHEN @s-srovn - @s-nula <0 THEN 'green' else CASE WHEN @s-srovn - @s-nula >0 THEN 'red' ELSE 'black' end endbut it tells me the expression does not work. Original formula was:CASE WHEN "D" - "B" <0 THEN 'green' else CASE WHEN "D" - "B" >0 THEN 'red' ELSE 'black' end endI just want to use variables to change part of the formula, but dont understand how. – Roman Dostál Sep 18 '22 at 13:23"B"and"D"are, where they're coming from etc. – Babel Sep 18 '22 at 21:21-and other special characters in variable names. only use ASCII characters (A-Z, a-z), numbers (0-9) + underscores. – Babel Sep 18 '22 at 21:23case when...is wrong. Don't add a secondcase when...statement in theelsecondition! This should work (avoing hyphens in variable names): `CASE WHEN @s_srovn - @s_nula < 0 THEN 'green'WHEN @s_srovn - @s_nula > 0 THEN 'red' ELSE 'black' end`
– Babel Sep 18 '22 at 21:28CASE WHENin the formula, I get an an error message of "Unexpected WHEN" etc. I have been using this way of writing it forever, and it always worked. Tried it your way and the same error I would expect happened. Thank you about the ASCII information. Anyway, I have tried to fill the value of the variable withB, with'B'and with"B"and I always get "Cannot convert B to double". I think it does not get the attribute but rather the string. Is there some setting that I need to change? B and D are integer number of car intensities on network, two scenarios. – Roman Dostál Sep 20 '22 at 11:53attribute_nameor enclosed in double quotes"attribute_name". – Babel Sep 20 '22 at 13:21layer_1. Variable (layer specific) isvar_1, the value isvalue_1, code iscase when @var_1 >800 then 'red' else case when @var_1 < 10 then 'blue' else 'green' end end. – Roman Dostál Oct 03 '22 at 11:33var_2with value ofvalue_2and change the code to:case when @var_1* @var_2 >800 then 'red' else case when @var_1* @var_2 < 10 then 'blue' else 'green' end endthen I get the same error message as before"Cannot convert 'value_1' to double". I think it somehow cannot get the correct format form the attributes. Even tried putting the value of the variable as a codeto_real(var_1)etc, but the same error - it seems to read it as a string. I am not sure about how the quotation works in the variable value. – Roman Dostál Oct 03 '22 at 11:40