There is a lot material on the community on explaining the difference between the functions SET and LET. The gist of it is: SET stores the variable with the expression string and LET will evaluate the expression first.

I tend to think of them as:

SET the value in place.

LET‘s evaluate.

 

But in reality, I hardly ever use SET. The whole difference of the them producing different results is on the premise that you don’t use quotes.

Take this example:



Set vSetToday = Today();
Let vLetToday = Today();

Trace ------------------------------- vSetToday: $(vSetToday);
Trace ------------------------------- vLetToday: $(vLetToday);

Which returns:

 

However, if you utilize quotes, the result is the exact same:



Set vSetTodayQuote = 'Today()';
Let vLetTodayQuote = 'Today()';

Trace ------------------------------- vSetTodayQuote: $(vSetTodayQuote);
Trace ------------------------------- vLetTodayQuote: $(vLetTodayQuote);

Which returns:

 

With that said, I argue that you should always use quotes. Using quotes is being explicit which I believe is tantamount in best practice coding. Also, you will notice Qlik’s generated variables¬† use quotes, so that’s another example straight from the source.

 

So my first point is use quotes!

 

So now that they are for the most part the same, is there even a reason to use SET? The only thing I could think of was performance reasons. To test this use case, I made a quick script to see if there was any tangible difference in using SET vs LET on setting a string variable.

This test does 5,000,001 iterations of setting a variable using each function and printing the result.



// SET Test

Let vSetStart = Now();

For s = 0 to 5000000

    SET vSetTest = '$(s)';

next s

Let vSetEnd = Now();

Let vSetDuration = Interval('$(vSetEnd)' - '$(vSetStart)','hh:mm:ss');

Let vLetStart = Now();

// LET Test
For l = 0 to 5000000

    LET vLetTest = '$(l)';

next l

Let vLetEnd = Now();

Let vLetDuration = Interval('$(vLetEnd)' - '$(vLetStart)','hh:mm:ss');

Trace ------------------------------- vSetDuration: $(vSetDuration);
Trace ------------------------------- vLetDuration: $(vLetDuration);

 

This test returns…..

 

 

LET is potentially faster, though I would say that it is at an amount that it doesn’t matter. It’s also worth noting that tests of 5,001, 50,001, and 500,001 were the same. However what this does prove, is that the biggest potential reason to use SET over LET doesn’t exist.¬† So given all that, just use LET.

LET is all you need.

 

…Except for Lists

At the moment I am aware of only one reason to use SET over LET and that is for building a list. You can create a list to iterate over using both, however using LET is much more painful. The LET statement you have to build in the quotes using Chr(39), which is not pretty and/or fun.

Example:


SET vSetArray = 'One','Two','Three';
LET vLetArray = chr(39)&'Four'&chr(39)&','&chr(39)&'Five'&chr(39)&','&chr(39)&'Six'&chr(39);

Trace ----------------- $(vSetArray);

For each s in $(vSetArray)

    Trace ----------------- $(s);

Next s

 

Trace ----------------- $(vLetArray);

For each l in $(vLetArray)

    Trace ----------------- $(l);

Next l

 

 

Tell me if there are if there are any other reasons for using SET over LET, because for now I think 99% of the time LET is all we need.