Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
hi,
what is the difference between using SET and LET in script? Can anyone explain with one example?
thanks
From the help:
The let statement, in opposition to the set statement, evaluates the expression on the right side of the '=' before it is assigned to the variable.
Set x=3+4;
Let y=3+4;
$(x) will be evaluated as '3+4 '
$(y) will be evaluated as '7'
Hi mariakm,
Try this it is very help full;
A variable is an entity used to contain a single data value which can be a number, a character or a string.
The variable acquires its value from LET or SET in the Script or from an Input Box in a layout which can be set by users.
There are few ways to create variables.
If the first character of the string starts with an equal sign ‘ = ‘, QlikView evaluates it as a formula and stores the evaluated result in it. Otherwise, it holds the value as a string. You can store a formula with equal sign in front, then you can just use the variable in the expression without Dollar Sign Expansion, if it is storing a string as a formula, then you need to use the Dollar Sign Expansion: ‘$(variablename)’.
Create a new Text Object and type ‘=vString1′, you will see ’1+3′ in the text object.
Then create another Text Object and type in ‘=$(vString1), then Dollar Sign Expansion evaluates the formula and you will get ’4′.
Then create another Text box and type in ‘=vString2′, you will see ’4′ in it.
‘LET’ and ‘SET’ are used for defining Variables in the script. The variables can be used for substituting strings, paths, drives, etc.
‘SET’ treats everything after the equal sign (=) as String. If it contains space, then you need double quote in the beginning and the end.
The syntax is: set variablename=string
Examples:
Set FileToUse=Data1.csv;
Set Constant=”My string”;
Set BudgetYear=1997;
‘LET’ evaluates the expression after the equal sign (=) and stores the value.
The syntax is: let variablename=expression (Note: the word ’let’ can be omitted. )
Examples
Let x=Today(); >> x=12/01/2012
Let y=’Today()’; >> y=Today()
Since the function, Today(), is encapsulated by the single quotes, QlikView treats it as a string. After running the script and from the Variable Overview in the Settings menu, you will find it is holding a string ‘Today()’.
In the Edit Script, if you are assigning variables with ‘Set’, QlikView will store whatever you typed in as a string. If you assigned a formula in a variable, and you want to use it in QlikView you need to use dollar sign expansion. However, if you assigned an equal sign in the beginning of the string, for example, “=Today() + 7″, you can just use the variable name without Dollar sign expansion since the variable will evaluate the formula and be represented as if it were a number.
If you are assigning variables with ‘Let’, you have to encapsulate the formula in between single quotes ( ‘ ). If you use double quotes, the variable gets a null value and the variable will not be created.
If you want to create a variable which hold a dollar sign expansion, then you need to separates the equation.
Examples
Let vStartDate = Today();
Let vEndDate = Date(Today()+7);
Let vSales = ‘Sum({$<Date={“>=$(vStartDate)<=$(vEndDate)”}>} Sales)’
If you run the script, you will get ‘Sum({$<Date={“>=12/1/2012<=12/8/2012″}>} Sales)’ for the variable vSales.
If you don’t want to expand the Dollar sign expansion part, you need to separate above formula in three parts.
Let vSales2 = ‘Sum({$<Date={“>=$’ & ‘(vStartDate)<=$’ & ‘(vEndDate)”}>} Sales)’
You will get ‘Sum({$<Date={“>=$(vStartDate)<=$(vEndDate)”}>} Sales)’ for the variable vSales2.
If you are using single quotes in the formula, then you need to use chr(39).
Let vSales3 = ‘Num(Sum({$<Date={“>=$’ & ‘(vStartDate)<=$’ & ‘(vEndDate)”}>} Sales), ‘ & chr(39) & ‘##,##0′& chr(39) & ‘)’
You will get ‘Num(Sum({$<Date={“>=$(vStartDate)<=$(vEndDate)”}>} Sales), ‘##,##0′)’.
That’s it.
SET Vstring3=1+3;
let Vstring4=1+3;
set Vif=if(year=2006,year);
Let Vstring5=today();//expression evaluates
let Vstring6='today()';//store string if u use $(Vstring6) evaluates expression
set Vstring7=today(); //store string if u use $(Vstring7) evaluates expression
set Vstring8="=today()";//evaluates expression
let Vstring9='=today()';//evaluates expression
let Ver='if(year=2006,year,0)';//use string and evaluates expression also
Let Vstring8="=today()"; //it's value is null variable not created.
hi
Set x=3+4;
Let y=3+4;
$(x) will be evaluated as 7
X will be evaluated as '3+4' string
y will be evaluated as '7'
Let's say we have the following...
Set A=5+6;
Let B=5+6;
$(A) will contain 5+6
$(B) will contain 11
in other words...
SET puts whatever after the = sign into the variable.
LET executes whatever after the = sign and load the variable with the result.
More or less.
Hi,
Set is a direct assignment.
Let is used when there is a function/operator to be evaluated on the right hand side before assigning the value.
This is inline with what Lucian and car bal have quoted, i guess I have just phrased it differently.
thanks,
Rajesh Vaswani
set x=3+4;
Let y=3+4
z=$(y)+1;
if u ignore Qlik view take let is default.Let is competitor of set.