SAS, 130 134
I got suggestion that use of macro variables for input is "invalid method", here is other version of the code which basically wraps the original one (look below) inside a %window
statement (makes it more obfuscated and limits it use to the DMS env. only):
The prompt window for DMS:
%window i a 4 b 4;%display i;
[Edit: some spaces were not necessary.]
The code that sums primes and prints result to the log
data;array a[&b](. 2:&b);do i=2to sqrt(&b);if a[i]then do j=2*a[i]by a[i]while(j<=&b);a[j]=.;end;end;x=sum(of a&a--a&b);put x;run;
Length of the code which "does the job" effectively stays the same.
Let me share a comment about "invalidity" of the first approach:
Passing input data in all 3 programming interfaces SAS DMS, SAS EG, or SAS Studio can be done:
- by a SAS dataset,
- by a text file,
- hard-coded inside data step, and
- by macrovariables.
A programmer does not have to wrap-up 4GL code inside a macro to use macrovariables in it, for example the following works perfectly well:
%let a=42;
data _null_;
the_answer=&a.;
put the_answer;
run;
The DMS allows window
and display
statements and they macro language counterparts %window
and %display
to produce a prompt input window. Unfortunately those are DMS "specific" and not working in the EG or SAS Studio interfaces.
Assuming we are working in the DMS env. For a SAS programmer execution of the following code, which creates and opens prompt input window:
%window in
@1 'a:' a 8 'b:' b 8;
%display in;
just before the data step and typing in values of a and b
is effectively equivalent to running:
%let a=XXX;
%let b=YYY;
with XXX
and YYY
as values for a and b.
The original post below:
input in SAS can be done by macrovariables:
%let a=3;
%let b=1000000;
the code sums primes and prints result to the log
data;array a[&b](. 2:&b);do i=2 to sqrt(&b);if a[i] then do j=2*a[i] by a[i] while(j<=&b);a[j]=.;end;end;x=sum(of a&a--a&b);put x;run;
more readable version, _null_
added to stop dataset creation:
%let a=3;
%let b=1000000;
data ;
array a[&b] (. 2:&b); /* array of integers from 2 to b */
do i = 2 to sqrt(&b); /* loop until sqrt of b */
if a[i] then /* if there is value in the array at i */
do j=2*a[i] by a[i] while (j<=&b);
a[j] = .; /* ... replace all multiplicities of a[i] with missings */
end;
end;
x = sum(of a&a--a&b); /* sum elements for position a to b */
put x;
run;