cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar
fat_angus
Level III

SAS to JSL - Do Loops...

I am a long time SAS user being forced to learn JSL... 

 

Things I found easy in SAS seem so difficult in JSL... 

 

Could someone show me how Do Loops work in JSL by sharing with me JSL code that would generate the following nested do loop table using JSL?  

 

data frustration;
  do temp = 1 to 10 by 1;
     do press = 5 to 20 by 5;
         do time = 1 to 1000 by 20;
         output;
         end;
    end;
  end;
run;

This should be fairly simple to create these types of simulations but I can't seem to figure this out. 

  

 

2 ACCEPTED SOLUTIONS

Accepted Solutions
pmroz
Super User

Re: SAS to JSL - Do Loops...

You've come to the right place for assistance learning JSL.  The scripting index is your friend, as is the on-line documentation.

Here's a code snippet that does what you want:

dt = data table("frustration");
for (temp = 1, temp <= 10, temp++,
	for (press = 5, press <= 20, press+=5,
		for (time = 1, time <= 1000, time+=20,
			show(temp, press, time);	// output
		);
	);
);

View solution in original post

pmroz
Super User

Re: SAS to JSL - Do Loops...

I find it easier to learn by example, so look at as much code as you can find in this forum.  Once you get going the scripting index will truly be your friend.

 

The JSL for loops and if statement, although simple in concept, were a bit baffling to me initially.  To shorten your learning curve about if, remember that it is a function, and the comma represents thenelse if and else.  Open a script window (ctrl-T), right-click in the window and select Show Embedded Log.  Paste the below code in and click ctrl-R to run it.  Play around with different values for a, b and c to see what happens.

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

if (a == b,
// then
	print("a is equal to b");
	,
// else if
	a == c,
// then
	print("a is equal to c");
	,
// else
	print("No match found for a");
);

 

View solution in original post

12 REPLIES 12
pmroz
Super User

Re: SAS to JSL - Do Loops...

You've come to the right place for assistance learning JSL.  The scripting index is your friend, as is the on-line documentation.

Here's a code snippet that does what you want:

dt = data table("frustration");
for (temp = 1, temp <= 10, temp++,
	for (press = 5, press <= 20, press+=5,
		for (time = 1, time <= 1000, time+=20,
			show(temp, press, time);	// output
		);
	);
);
fat_angus
Level III

Re: SAS to JSL - Do Loops...

Thanks for the help pmroz

txnelson
Super User

Re: SAS to JSL - Do Loops...

All of this is covered in the Scripting Guide, which I very, very strongly recommend you read.  It is available in the JMP Documentation Library, available under the Help pull down menu in JMP.

Jim
fat_angus
Level III

Re: SAS to JSL - Do Loops...

I hear what you are saying... thank you... but, it's kind of like someone telling you to read the Bible to understand Christianity....I spent about 2 hours in the scripting guide looking for how to do this seemingly easy concept... additionally, I have had lots of success writing other scripts... but honestly, the scripting guide makes me yearn for an additional 20k in the budget for a SAS license. 

 

I am not a hardcore coder... I am an engineer just trying to get stuff done fast. 

pmroz
Super User

Re: SAS to JSL - Do Loops...

I find it easier to learn by example, so look at as much code as you can find in this forum.  Once you get going the scripting index will truly be your friend.

 

The JSL for loops and if statement, although simple in concept, were a bit baffling to me initially.  To shorten your learning curve about if, remember that it is a function, and the comma represents thenelse if and else.  Open a script window (ctrl-T), right-click in the window and select Show Embedded Log.  Paste the below code in and click ctrl-R to run it.  Play around with different values for a, b and c to see what happens.

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

if (a == b,
// then
	print("a is equal to b");
	,
// else if
	a == c,
// then
	print("a is equal to c");
	,
// else
	print("No match found for a");
);

 

fat_angus
Level III

Re: SAS to JSL - Do Loops...

Thanks SUPER USER PMROZ once again; this is helpful. 

Craige_Hales
Super User

Re: SAS to JSL - Do Loops...

C++ Programmer's Guide to JSL  might be of some help too. It tries to show how the JSL for statement is really a function, and knowing that will make it a lot easier to get the syntax right.

Craige
fat_angus
Level III

Re: SAS to JSL - Do Loops...

THank you... this is helpful. 

Craige_Hales
Super User

Re: SAS to JSL - Do Loops...

A complete example, appreciating how hard it is to get started in a new language. (The help->scripting index and the online help are complementary resources. I mostly use the help->scripting index, for reminding me how it works. The online help might be better for starting out.) I put a little more here than just needed. I've forgotten SAS rules for case of names...

 

Names Default To Here( 1 );
// what does that mean? JSL has several namespaces; the 'here' namespace is for this
// editor window and means 'press' is really 'here:press' but 'dtFrustration:press' belongs to
// the table's namespace (of column names.) You could also just use different names,
// like 'iPress' for the loop control variable. JMP's variable names are almost never
// case sensitive, the weird exception being column names, which are only sensitive 
// if both 'Age' and 'age' are columns...Don't do that! // an easy way to get this table script is to make a new table with the // JMP GUI, then use the top-left red triangle to 'Copy Table Script (No Data)'. // By convention, JSL uses 'dt' in the variable name that holds a handle to a table. dtFrustration = New Table( "frustration", New Column( "temp" ), // continuous numeric by default New Column( "press" ), New Column( "time" ) ); // because the table might be linked to other reports, there // is a lot of hidden messaging when the table changes. A lot. // The messaging can be shut off and later enabled for a nice speedup. // (to see it take longer, rerun just the loop below.) // dtFrustration << BeginDataUpdate; // pair with EndDataUpdate, below For( temp = 1, temp <= 10, temp++, For( press = 5, press <= 20, press += 5, For( time = 1, time <= 1000, time += 20, dtFrustration << addrows( 1 ); dtFrustration:time = time; dtFrustration:temp = temp; dtFrustration:press = press; ) ) ); // dtFrustration << EndDataUpdate;
Craige