Subscribe Bookmark RSS Feed

If statements in JSL

Is it possible to execute multiple statements from one if condition - like an if/then/do statement in SAS? How can it be done?
1 ACCEPTED SOLUTION

Accepted Solutions
thechadd

Community Trekker

Joined:

Jun 23, 2011

Solution

You bet. It's odd in JSL, but semicolons are subordinate to commas. So you can have statements in an if statement separated by semicolons.

If(,<statements separated by semicolons>).

7 REPLIES
thechadd

Community Trekker

Joined:

Jun 23, 2011

Solution

You bet. It's odd in JSL, but semicolons are subordinate to commas. So you can have statements in an if statement separated by semicolons.

If(,<statements separated by semicolons>).

JSL gives the appearance of a regular language with statements terminated by a semicolon. It is however, very much a function driven language, where you make function calls with lists of arguments separated by commas. Standard programming constructions such as loops and if statements are implemented as functions. The IF function can take multiple arguments, which act in condition expression-pairs:

If ( condition1, expression1, condition2, expression2, ..., final-expression )

Thus giving the structure of an if--elseif-else statement.

Each expression can consist of multiple lines of code delineated with a semicolon.

In fact, the semicolon is just an operator that implements the Glue function. Glue takes a list of JSL expressions separated by commas,

so for example:

a=1;
b=2;
c=a+b;


is just short-hand for

Glue(a=1,b=2,c=a+b)

All the JSL functions are listed with descriptions in the JSL Functions Index under Help>Indexes
pmroz

Super User

Joined:

Jun 23, 2011

The IF statement in JSL takes a little getting used to. For readability I use comments that mark the "THEN" and "ELSE" sections.

Regards,

Peter

thechadd

Community Trekker

Joined:

Jun 23, 2011

These boards take HTML?????

Wahoo!!!
pmroz

Super User

Joined:

Jun 23, 2011

I put a very brief description of how to post formatted code in this posting:

http://support.sas.com/forums/message.jspa?messageID=38882#38882

Regards,
Peter
thechadd

Community Trekker

Joined:

Jun 23, 2011

Wow, that's awesome, thanks! (sorry for hijacking this thread).
Badri

Community Member

Joined:

Feb 3, 2017

All actions need to be separated by semicolon and then grouped using parenthesis.

The following example worked.

 

A=1;

if(A==1, (x=A*1; y=A*2;z=A*3), (x=A^1; y=A^2;z=A^3));

Print(x); Print(y); Print(z);

/*:

 

1

2

3

//:*/

A=2;

if(A==1, (x=A*1; y=A*2;z=A*3), (x=A^1; y=A^2;z=A^3));

Print(x); Print(y); Print(z);

 

/*:

 

2

4

8