# Javascript is not SAS

I decided I need to learn a new language. I had learned a number of languages in the past but SAS is the only one I used on a daily basis. I write SPSS syntax about every other day (does that count?). I toyed with Ruby and C++.  I listened, bemusedly, to people who exhorted me to learn R. In the end, I settled on Javascript and we are having a good relationship.

My choice was based on needing something that was better at stuff SAS isn’t very good at. I needed something that would create dynamic web pages, was easy for user interaction. Also, I think it is good to get out of your comfort zone from time to time. Now js and I are on an initials basis. There are a few ways that I still frequently get thrown off, though.

Arrays subscripts.

In SAS, to refer to the ith value in an array named bob, I use bob{i}

In javascript, you’d use  bob[i]

I have had to delete the curly brackets and replace them with square ones approximately 40 zillion times

Also, arrays begin with 0 in javascript and 1 with SAS. So, every index value is one less in js. The third element of bob is bob[2]

For some reason, that was far easier for me to adapt to than the bracket difference. A reasonable person might say this is wrong and argue that counting starts at one.

I went back to apples. If you are counting apples, you start at 1. If you are measuring distance, you start at zero. When you start to go somewhere you are not 1 mile or 1 foot or one anything from your house. You are at point zero.

Javascript = house

SAS = apple

DO loops – in a good way

Another way javascript is different is do loops.

Take this  example of how you would do a loop in SAS:

DO i = 1 to DIM(bob) ;

some programming stuff ;

END ;

Then, at the end of your program, you need to delete i or it sticks around forever. In javascript, you do this;

for (i=0; i < facts.length ; i++) {

some programming stuff

}

Because you set the counter to 0 at the beginning, you can re-use the same counter variable over and over in your program. Of course, you could re-do this in SAS by re-setting i to 1 after every DO loop but that takes an extra statement. I usually just use different variable names – i,j, k – and drop them all in the DROP statement at the end of my program.

The moral of the day is, if you are a SAS programmer and are interested in picking up a new language, there are certainly worse choices than javascript. Much of what you are comfortable with from SAS will transfer. However, there are the odd differences, some of which, like the different type of brackets for arrays, which will make your life difficult and others, like the for style for DO loops that can make your life easier.

I have more to say about SAS and javascript and why I chose javascript but for now, I need to write a post on why Codecademy is not the way to learn to code.

**** Thank you to @stat_geek on twitter who tweeted that you could also use [] in SAS and they would work. My point is that I have always used {} and have never used the square brackets and old habits are hard to break. Tomorrow I am going to have to change some of my SAS programs to use [] just so I can be one of the cool kids.

## Similar Posts

1. Gabe says:

The array difference in javascript has always bugged me too.

Yes, you’re correct that if you’re measuring distance, you start at zero. However, distance is a continuous, not discrete, measurement.

On the other hand array items are, by definition, discrete. There’s no such thing as the zeroth item of an array.

2. Richard says:

A long time ago I used to use {} but changed to [] when I figured it was easier to type! You can use () but that would be just too confusing. I’ve also noticed a fashion of using _n_ instead of i for the counter in do loops as _n_ is never kept. Can’t see the point as it involves a lot more typing and I hardly ever drop the i anyway.
R

3. Quentin says:

Hi,
Nice post. Just a couple thought re SAS arrays. You can specify that SAS arrays use 0 as the lower bound instead of 1:

211 data a;
212 array vars {0:9};
213 do i=0 to 9;
214 vars{i}=ranuni(0);
215 end;
216 drop i;
217 run;

NOTE: The data set WORK.A has 1 observations and 10 variables.

I think it might even have some efficiency advantages.

SAS also lets you re-use counters when you code an iterative do-loop, cuz the counter is automatically set to the lower bound at the top of the loop. e.g:

219 data a;
220 do i=1 to 3;
221 put i=;
222 end;
223 do i=1 to 2;
224 put i=;
225 end;
226 run;

i=1
i=2
i=3
i=1
i=2

I think the only time you have to worry about reusing a counter is when you have a loop inside another.

4. Gabe says:

Quentin –

In your first example, the variables created by the array statement are named vars1, vars2, vars3, …, vars10. This is despite explicitly setting “{0:9}”. I think that gets confusing, since vars2, the second element of the array, is referred to by i=1, vars3, the third element of the array, is referred to by i=2, etc.

5. Thea says:

“I need to write a post on why Codecademy is not the way to learn to code.”

My sentiments exactly! I recently picked up Java in order to learn how to write code for mobile applications, and I use Matlab on a daily basis and C++ occasionally. I have to say that 90% of coding requires tinkering, which I really don’t see a model like Codeacademy teaching.

6. Quentin says:

Gabe- That is true when you ask SAS to create the variables and don’t name the variables yourself. You can always explicitly name the variables:

12227 data a;
12228 array vars {0:9} var0-var9;
12229 do i=0 to 9;
12230 vars{i}=ranuni(0);
12231 put vars{i}=;
12232 end;
12233 drop i;
12234 run;

var0=0.207716575
var1=0.9773393543
var2=0.7684478493
var3=0.7777999173
var4=0.4570779695
var5=0.7801388534
var6=0.4467696689
var7=0.5735065907
var8=0.7666515167
var9=0.093267133
NOTE: The data set WORK.A has 1 observations and 10 variables.