Hello
what is the reason for the error please?
35 DROP AAA{i};
_
22
76
ERROR 22-322: Syntax error, expecting one of the following: a name, -, :, ;, _ALL_, _CHARACTER_, _CHAR_, _NUMERIC_.
ERROR 76-322: Syntax error, statement will be ignored.
36 rename BBB{i}=AAA{i};
_
22
76
ERROR 22-322: Syntax error, expecting one of the following: -, :, =.
ERROR 76-322: Syntax error, statement will be ignored.
37 end;
38 run;
data want;
set have;
array AAA {12} Revenue1_sum Revenue2_sum Revenue3_sum Revenue4_sum Revenue5_sum Revenue6_sum
Revenue7_sum Revenue8_sum Revenue9_sum Revenue10_sum Revenue11_sum Revenue12_sum;
array BBB {12} Revenue1_sum_ Revenue2_sum_ Revenue3_sum_ Revenue4_sum_ Revenue5_sum_ Revenue6_sum_
Revenue7_sum_ Revenue8_sum_ Revenue9_sum_ Revenue10_sum_ Revenue11_sum_ Revenue12_sum_;
do i = 1 to 12;
if AAA{i} <0 and AAA{i} ne . then BBB{i} =0;
DROP AAA{i};
rename BBB{i}=AAA{i};
end;
run;
It's frustrating that we have to request this over and over from you, but please post the log in the window that appears when you click on the </> icon. This preserves the formatting of the log and makes it much more readable.
Also, don't be stingy, show us the entire log for this DATA step, not selected lines.
Unfortunately, this kind of syntax is not supported. You can not reference an array, or part of an array in a Drop or Rename Statement.
DROP AAA{i};
rename BBB{i}=AAA{i};
The data step looks as if you should read the documentation explaining the statements "drop" and "rename". Hint: it is not possible to execute them conditionally.
DROP and RENAME are compile-time statements, when i has not yet a value, so how should the data step compiler know which array element to drop?
You can list variables using short cut lists which rely on a prefix. Using varying suffixes this type of operation is not supported and bad design given that SAS works well with prefixes.
So redesign your process so you have unique prefixes and then drop them using the colon operator or - or -- notation.
AKA rename your array variables such that they have unique prefixes.
Here is a reference that illustrates how to refer to variables and datasets in a short cut list:
https://blogs.sas.com/content/iml/2018/05/29/6-easy-ways-to-specify-a-list-of-variables-in-sas.html
@Ronein wrote:
So what is the way to drop and rename 12 variables in efficiency way without writing long code?
What modification is required to my code to get the desired results?
Like that:
data work.have;
array AAA {12}
Revenue1_sum Revenue2_sum Revenue3_sum Revenue4_sum Revenue5_sum Revenue6_sum
Revenue7_sum Revenue8_sum Revenue9_sum Revenue10_sum Revenue11_sum Revenue12_sum;
do i = 1 to 3;
do j = 1 to 12;
t+1;
AAA[j] = t;
end;
output;
end;
run;
proc print data = work.have;
run;
proc sql;
select cats(name,"=",name,"_")
into :list separated by " "
from dictionary.columns
where libname = "WORK"
and memname = "HAVE"
and upcase(name) like 'REVENUE%'
;
quit;
proc datasets lib = work;
modify have;
rename &list.;
run;
quit;
proc print data = work.have;
run;
?
Bart
Or just drop the second array entirely as well as the rename/drop. It's not required in the code you've shown.
Need only one array to check and replace. Reeza has already said this. I give an illustration with 3 cells.
data have;
Revenue1_sum = 1;
Revenue2_sum = -0.1;
Revenue3_sum = -0.2;
run;
data want;
set have;
array aaa[3] Revenue1_sum Revenue2_sum Revenue3_sum;
do i = 1 to 3;
if aaa[i] < 0 and aaa[i] ne . then aaa[i] = 0;
end;
drop i;
run;
proc print data = want;
run;
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.