Skip to main content
Woohoo! Qlik Community has won “Best in Class Community” in the 2024 Khoros Kudos awards!
Announcements
Nov. 20th, Qlik Insider - Lakehouses: Driving the Future of Data & AI - PICK A SESSION
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

use of SET and LET in script

hey

as i am new to qlikview will you please tell me whtat is the use and when to use SET and LET in edit script...

7 Replies
Not applicable
Author

PFA.


CELAMBARASAN
Partner - Champion
Partner - Champion

LET and SET

Check with the above discussion

Not applicable
Author

HI CHECK THIS

With the distinction between SET and LET, where SET does not evaluate the RHS and LET evaluates the RHS, the following lists the behavior that we think is not consistent:

Example 1:

SET A = 'hello';

SET B = customer = 'hello';

Both A and B should preserve the single quotes. Currently Qlikview removes the single quote for the variable A.

Example 2:

SET A = 5;

SET B = $(A);

I expect that B contains the string: $(A). Instead, it contains 5 because the dollar-sign gets expanded. This will be useful if I want to specify a list of variables that contain formulae.

Not applicable
Author

HI STUDY THIS

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.

  1. 1. Variables Tab in the Document Properties
  2. 2. New Variables in New Input Box Properties
  3. 3. Variable Overview in Settings Menu
  4. 4. Using ‘SET’ or ‘LET’ in Edit Script

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)’.

  1. i.e. Let’s create two variables: vString1 and vString2.  vString1 stores a string ’1+3′ and vString2 stores a string ‘=1+3′.

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.

Not applicable
Author

Very Clear, detailed and easy to understand. Thanks you very much!

nizamsha
Specialist II
Specialist II

for simple understanding Set =5+3 mean it wont evaluate when the script runs u  have to call again to make it as 8

but when comes to LET =5+3 first time it will evaluate and produce the result as 8

use this eg and run the script and open the variable and check the answer

MuraliPrasath
Creator III
Creator III

   SET Vs LET


                   Using SET you are adding string value to a variable
SET Variable1='4+5';
variable value is '4+5'

    Using LET you are adding an expression result to a variable
LET Variable1=4+5;
variable value is 9