I am trying to run a %cif macro but I am getting the following error:
ERROR: (execution) Invalid argument to function.
count : number of occurrences is 2
operation : EXP at line 1436 column 1
operands : _TEM1008
_TEM1008 7851 rows 1 col (numeric)
statement : ASSIGN at line 1436 column 1
I have only been using SAS for a few months, so would be grateful is someone could point me in the right direction.
Related
A program that loads and processes command-line arguments should be created.
Here comes a few examples on how it should look when you run it (bold text is the text that the user will type):
Terminal prompt % **./my_program**
No arguments given.
Terminal prompt % **./my_program 123**
Wrong amounts of arguments given.
Terminal prompt % **./my_program 10 XYZ 999 Greetings!**
Wrong amounts of arguments given.
Terminal prompt % **./my_program 3 HELLO**
Message: HELLOHELLOHELLO
The program "./my program" is ending.
Terminal prompt % **./my_program 0 Bye**
Message:
The program "./my program" is ending.
This is my code so far:
with Ada.Text_IO; use Ada.text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Ada.Command_Line; use Ada.Command_Line;
procedure my_program is
type String is array (Positive) of Character;
N : Integer;
Text : String;
begin
N := Argument_Count;
if N = 0 then
Put_Line("No arguments given.");
elsif N /= 2 then
Put_Line("Wrong number of arguments given.");
elsif N = 2 then
Put("Message: ");
for I in 1 .. N loop
Put(Text);
New_Line;
end loop;
Put("The program """);
Put(""" is ending. ");
end if;
end my_program;
My program handles the first 3 three cases but when I go ahead with the 4th and 5th (last) case I get an error code at the row Put(Text) where it says
Missing argument for parameter "Item" in call to "Put"
I don't know if I declared my string right because I don't want a string of a specific length. Can anyone come up with something that could help me solve case 4 and 5? It would be nice and highly appreciated
This seems to be a homework or exam question, so I would usually not provide a full answer. But Chris already gave that (with some defects), so here is my suggestion. Compared to Chris's solution, I try to avoid using unnecessary variables, and I favour case statements over if-then-else cascades, and I try to reduce the scope of exception handlers. I prefer to put use clauses in the subprogram so that the context-clause section contains only with clauses. I use the string-multiplying "*" operator from Ada.Strings.Fixed, but that is perhaps an unnecessary refinement.
with Ada.Command_Line;
with Ada.Strings.Fixed;
with Ada.Text_IO;
procedure My_Program
is
use Ada.Strings.Fixed;
use Ada.Text_IO;
begin
case Ada.Command_Line.Argument_Count is
when 0 =>
Put_Line ("No arguments given.");
when 2 =>
begin
Put_Line (
Natural'Value (Ada.Command_Line.Argument(1))
* Ada.Command_Line.Argument(2));
exception
when Constraint_Error =>
Put_Line ("Invalid input for argument 1.");
end;
when others =>
Put_Line ("Wrong amount of arguments given.");
end case;
Put_Line (
"The program """
& Ada.Command_Line.Command_Name
& """ is ending.");
end My_Program;
Note that my version:
Rejects negative first arguments (like "-3").
Outputs the repeated strings on a single line, as was required by the examples given.
Includes the name of the program in the final message, as was also required.
Given the clarification in comments as to the purpose of the program, to print a message n times where n is the first argument, and the message is the second argument, you need to parse the first argument as an integer. This can be done with Integer'Value.
Now, that raises the prospect of the user not running the program with an integer. So we have to handle the possible Constraint_Error exception.
with Ada.Text_IO; use Ada.text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Ada.Command_Line; use Ada.Command_Line;
procedure my_program is
argc : Integer;
N : Integer;
begin
argc := Argument_Count;
if argc = 0 then
Put_Line("No arguments given.");
elsif argc /= 2 then
Put_Line("Wrong number of arguments given.");
else
n := Integer'Value(Argument(1));
Put("Message: ");
for I in 1 .. N loop
Put_Line(Argument(2));
end loop;
Put("The program """);
Put(""" is ending. ");
end if;
exception
when Constraint_Error =>
Put_Line("Invalid input for argument 1.");
end my_program;
As an aside, when we've checked in our conditional if argc is zero, and that it doesn't equal two, we don't have to use elsif. The only other possibility is that it is 2.
You say
My program handles the first 3 three cases but when I go ahead with the 4th and 5th (last) case I get an error code at the row Put(Text) where it says "Missing argument for parameter "Item" in call to "Put". "
which doesn't make sense, because your program as shown doesn't compile. I guess what you mean is "when I try to add the code to handle cases 4 and 5, it doesn't compile".
The reason why it doesn’t compile is hidden in the actual error messages:
leun.adb:24:10: no candidate interpretations match the actuals:
leun.adb:24:10: missing argument for parameter "Item" in call to "put" declared at a-tiinio.ads:97, instance at a-inteio.ads:18
...
leun.adb:24:14: expected type "Standard.Integer"
leun.adb:24:14: found type "String" defined at line 7
leun.adb:24:14: ==> in call to "Put" at a-tiinio.ads:80, instance at a-inteio.
You have at line 7
type String is array (Positive) of Character;
which is both misleading and not what you meant.
It’s ’not what you meant’ because array (Positive) means an array of fixed length from 1 to Positive’Last, which will not fit into your computer’s memory. What you meant is array (Positive range <>).
Even with this correction, it's 'misleading' because although it would be textually the same as the declaration of the standard String in ARM 3.6.3(4), in Ada two different type declarations declare two different types. So, when you write Put(Text); the Put that you meant to call (the second in ARM A.10.7(16)) doesn’t match because it’s expecting a parameter of type Standard.String but Text is of type my_program.String.
Cure for this problem: don’t declare your own String type.
I have the following SAS code:
data part1;
set current.part;
by DEVICE_ID part_flag_d
if first.DEVICE_ID or first.part_flag_d;
ITEM_NO = 0;
end;
else do;
ITEM_NO + 1;
end;
run;
I am converting this to PySpark and getting stuck. I have the 'part' DataFrame. Where I am getting stuck is trying to convert the following line:
if first.DEVICE_ID or first.part_flag_d;
I know it's getting the first entry of each column, but is it also checking for null? What is the OR condition saying?
Would appreciate any direction on how to script that line.
Please refer to the documentation for the by statement:
http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/a000202968.htm#a002651313
Processing BY Groups
SAS assigns the following values to
FIRST.variable and LAST.variable:
FIRST.variable has a value of 1 under the following conditions:
when the current observation is the first observation that is read
from the data set.
when you do not use the GROUPFORMAT option and the internal value of
the variable in the current observation differs from the internal
value in the previous observation.
If you use the GROUPFORMAT option, FIRST.variable has a value of 1
when the formatted value of the variable in the current observation
differs from the formatted value in the previous observation.
FIRST.variable has a value of 1 for any preceding variable in the BY
statement.
In all other cases, FIRST.variable has a value of 0.
LAST.variable has a value of 1 under the following conditions:
when the current observation is the last observation that is read from
the data set.
when you use the GROUPFORMAT option and the internal value of the
variable in the current observation differs from the internal value in
the next observation.
If you use the GROUPFORMAT option, LAST.variable has a value of 1 when
the formatted value of the variable in the current observation differs
from the formatted value in the next observation.
LAST.variable has a value of 1 for any preceding variable in the BY
statement.
In all other cases, LAST.variable has a value of 0.
Please see the code below. It is generated by an automated conversion tool called SPROCKET you can find the info about it here
It should take care of the first.variable
# DATASTEP
part1 = (
CURRENT_part_df # change to your set df name
.withColumn('_first_DEVICE_ID', when(
row_number().over(Window.partitionBy(['part_flag_d']).orderBy('DEVICE_ID')) == 1, 1)
.otherwise(lit(0)))
# WARNING: NO ORDER DETECTED, MANUALLY SPECIFY ORDER VARIABLE
.withColumn('_first_part_flag_d', when(
row_number().over(Window.partitionBy(['DEVICE_ID','part_flag_d']).orderBy("")) == 1, 1)
.otherwise(lit(0)))
.withColumn('ITEM_NO', expr("""case
when (_first_DEVICE_ID > 0 or _first_part_flag_d > 0) then 0
when not ((_first_DEVICE_ID > 0 or _first_part_flag_d > 0)) then ITEM_NO + 1
else ITEM_NO end"""))
.drop('_first_DEVICE_ID')
.drop('_first_part_flag_d')
)
This question already has an answer here:
Suppressing Output MATLAB
(1 answer)
Closed 6 years ago.
Trying to understand this code :
A = [1 2 3]
T = A(:,1:end);
fprintf('\nvalues ', T);
A creates a Matrix of dimension 1 x 3
When I run this code, this is printed :
A =
1 2 3
Why is T not implicitly evaluated and printed to screen ?
I'm unfamiliar with this syntax : A(:,1:end); is this selecting the first column of Matrix and looping ?
The lines of code that are evaluated in screen "implicitly" are the ones that do not end with ;. The semicolon operator suppresses the printing of the result of that line.
In your code,
A = [1 2 3] % No semicolon -> print
T = A(:,1:end); % semicolon -> no print
The end keyword has nothing to do with the printing. Its a keyword very useful to do vetorized operations in Matlab.
Saying A(:,1:end) you are telling MATLAB " take all the values (:) from column indexes starting on 1 until the last column of the matrix end. Basically, in this case, all the values of A. You can try A(1:end,1:end) and check that returns the same thing.
For a more useful example, you might want all the matrix but the first row, then you would use A(2:end,:).
I am attempting to that the natural log of a number, I get the message:
tf2 = 60*ln(B1);
Undefined function 'ln' for input arguments of type 'double'.
So i try to cast the number as a float which the documentation claims it will accept but
then i get the error message :
float(B1);
Error using float (line 50)
The input argument to float was not a supported type. The only recognized strings are 'single' and 'double'. The input type was 'double'
So then i try to cast the double as a single and get the same error but it says :
f=single(B1);
float(B1);
Error using float (line 50)
The input argument to float was not a supported type. The only recognized strings are 'single' and 'double'. The input type was 'single'
The natural log in MATLAB is simply log(x). You're mixing the two:
log in MATLAB
log in MuPAD
The error message you get is because the function is not defined. You'll get the same error for this line:
bogus_function(1.23)
??? Undefined function or method 'bogus_function' for input arguments
of type 'double'.
I know it's an old question but as I didn't find a good answer when I was trying to do it so I will write my solution for others.
First there is no implemented function to do ln operation in matlab, but we can make it.
just remember that the change formula for log base is
log b (X)= log a (X)/log a (B)
you can check this easily.
if you want to calculate log 2 (8)
then what you need to do is to calculate log 10 (8)/log 10 (2)
you can find that: log 2 (8) = log 10 (8)/log 10 (2) = 3
So easily if you want to calculate ln(x), all you need is to change the base to the e.
ln(x) = log 10 (x)/log 10 (e)
so, just write that code in matlab
my_ln= log 10 ( number ) / log 10 ( exp(1) );
you can also make it as a function and call it whenever you need it,
function [val] = ln_fun(number)
val = log 10 (number)/ log 10 ( exp(1) );
end
*remember the log general formula → log base (number)
I wrote this code inside the M Blcok (one of Xilinx blockstes in Simulink):
function z= discorr(x,y)
t=zeros(12288,1);
i=zeros(12288,1);
k=zeros(12288,1);
i(4096:8191,1)=x(1:4096,2); %output of the image filter
t(4096:8191,1)=y(1:4096,2); %output of the tamplate filter
i=i';
z=A(1:4096,1);
for n=1:8191
k=zeros(12288,1);
k(n:n+4095,1)=t(4096:8191,1);
z(n,2)=i*k;
end
end
Its telling me:
Error("discreatcorr.m"): Syntax error: Lexical error at line 15, column 0. Encountered: after : "\';\r\nz=A(1:4096,1);\r\nfor n=1:8191\r\n k=zeros(12288,1);\r\n k(n:n+4095,1)=t(4096:8191,1);\r\n z(n,2)=i*k;\r\n end\r\nend\r\n"
Error("discreatcorr.m"): Syntax error: Lexical error at line 15, column 0. Encountered: after : "\';\r\nz=A(1:4096,1);\r\nfor n=1:8191\r\n k=zeros(12288,1);\r\n k(n:n+4095,1)=t(4096:8191,1);\r\n z(n,2)=i*k;\r\n end\r\nend\r\n"
Error occurred during "Block Configuration".
althogh there is nothing in line 15 in the code
it is giving an error at the end of the code
Any Ideas??
The problem is that your system misinterprets the ' symbol as string symbol. Replacing the line i=i'; by i=transp(i); should solve the problem.