Hello,
I have a sas data set called salary1 in the library called cert with 1 observation and 2 variables (salary and year). I want to create an output salary2 in the library called results by increasing salary by 5.65% until I have $500.000. The value of salary =256000.45 and year=2022.
I get an error message when I submit the code below. The error is on ‘Salary *0.0565’
Data results.salary2;
Set cert.salary1;
Do until (salary>=500000);
Salary=256000.45
Salary *0.0565 ;
Year + 1;
End ;
Proc print data = results.salary2;
Run ;
Any help?
Here are some additional explanations and modified code.
SAS basically uses the form "variable=value to be assigned;" when assigning a value to a variable.
In the following, the assignment is correct, but the semicolon at the end of the statement is missing.
Salary=256000.45
The error message is missing the semicolon at the end of the statement, so the next line is considered a single statement, and you see a keyword in the error message that connects Salary to 256000.45 as "expecting one of the following: !, !!, &, *, **, +, -,...".
Salary=256000.45 Salary *0.0565 ;
Also, the following does not match the syntax "variable=value to be assigned;" which is problematic.
Salary *0.0565 ;
Therefore, the following needs to be corrected
Salary=Salary*0.0565 ;
However, you said " by increasing salary by 5.65% ".
So I think the following should be written here
Salary=Salary+Salary*0.0565 ;
Note that the following statement is "sum statement", which is a special statement to calculate the sum between obs, so there is no syntax problem.
Year + 1;
I suggest this code.
data results.salary2;
set cert.salary1;
do until (salary>=500000);
Salary = Salary + Salary * 0.0565 ;
Year + 1;
end;
run;
As you have discovered, this statement is invalid:
Salary *0.0565 ;
In addition, your logic requires a tweak. The initial value must be set before entering a loop. For example:
Data results.salary2;
Set cert.salary1;
Salary = 256000.45;
Year = 0;
Do until (salary>=500000);
Salary = Salary *0.0565 ;
Year + 1;
end ;
run;
Proc print data = results.salary2;
Run ;
Finally, note that SALARY begins with the same value on each observation. (Perhaps that is a mistake assigning SALARY a value and the actual SALARY should be read from the input data set.) At any rate, the final value for SALARY and YEAR will be unchanging from one observation to the next.
Here are some additional explanations and modified code.
SAS basically uses the form "variable=value to be assigned;" when assigning a value to a variable.
In the following, the assignment is correct, but the semicolon at the end of the statement is missing.
Salary=256000.45
The error message is missing the semicolon at the end of the statement, so the next line is considered a single statement, and you see a keyword in the error message that connects Salary to 256000.45 as "expecting one of the following: !, !!, &, *, **, +, -,...".
Salary=256000.45 Salary *0.0565 ;
Also, the following does not match the syntax "variable=value to be assigned;" which is problematic.
Salary *0.0565 ;
Therefore, the following needs to be corrected
Salary=Salary*0.0565 ;
However, you said " by increasing salary by 5.65% ".
So I think the following should be written here
Salary=Salary+Salary*0.0565 ;
Note that the following statement is "sum statement", which is a special statement to calculate the sum between obs, so there is no syntax problem.
Year + 1;
I suggest this code.
data results.salary2;
set cert.salary1;
do until (salary>=500000);
Salary = Salary + Salary * 0.0565 ;
Year + 1;
end;
run;
Thank you for your input!
Maxim 2: Read the Log.
69 Data results.salary2;
70 *Set cert.salary1;
71 Do until (salary>=500000);
72 Salary=256000.45
73 Salary *0.0565 ;
______
22
ERROR 22-322: Syntaxfehler, erwartet wird eines der folgenden: !, !!, &, *, **, +, -, /, <, <=, <>, =, >, ><, >=, AND, EQ, GE, GT,
LE, LT, MAX, MIN, NE, NG, NL, OR, ^=, |, ||, ~=.
The SAS data step provides a SUM statement (e.g. salary + x;), but not a MULT statement.
So your statement must be a complete assignment statement:
salary = salary * 0.0565;
Bu then, your code would run into an infinite loop:
do until (salary >= 500000);
salary = 256000.45;
salary = salary * 0.0565;
year + 1;
end;
At the end of the loop, salary would always be 256000.45 * 0.0565, so it will never change.
Now, even if you move the initialization before the loop
salary = 256000.45;
do until (salary >= 500000);
salary = salary * 0.0565;
year + 1;
end;
you will still get an infinite loop, as the multiplication will lower the value of salary, so it again never reaches the termination value.
If you want to increase a value by 5.65 %, you need to multiply the value by 1.0565.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.