*I want to print out any of the tree_: which falls under any of the following types of trees '999', '888' etc.;
*The following code I have used;
%let trees = %nrstr ('999', '888', '111', '222', '555', '876', '564', '109',
'456', '543', '897', '009', '321'); *I will check many more types of trees like the '999' etc.;
data want;
set have;
array hl {*} tree_:; *here tree_: can be even up to 50 (tree_50) and it is not fixed;
tree_ = &trees;
do i=1 to dim(hl);
if hl(i) ne &trees then tree_ = 0;
tree = whichc('&trees', of hl(*))>0;
end;
run;
The following is what showing in log, there were some numbers at the beginning of each SAS statement that I have replaced with *** as I am not sure if I can publish it;
****%let trees = %nrstr('999', '888', '111', '222', '555', '876', '564', '109',
'456', '543', '897', '009', '321');
**** data want;
**** set have;
**** array hl {*} tree_:;
**** tree_ = &trees;
NOTE: Line generated by the macro variable "trees".
1 '999', '888', '111', '222', '555', '876', '564',
-
388
-
76
1 ! '456', '543', '897', '009', '321'
ERROR 388-185: Expecting an arithmetic operator.
ERROR 76-322: Syntax error, statement will be ignored.
**** do i=1 to dim(hl);
**** if hl(i) ne &trees then tree_ = 0;
NOTE: Line generated by the macro variable "trees".
1 '999', '888', '111', '222', '555', '876', '564',
-
388
-
76
1 ! '456', '543', '897', '009', '321'
ERROR 388-185: Expecting an arithmetic operator.
ERROR 76-322: Syntax error, statement will be ignored.
**** tree = whichc('&trees', of hl(*))>0;
**** end;
**** run;
NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.want may be incomplete. When this step was stopped there were 0
observations and *** variables.
WARNING: Data set WORK.want was not replaced because this step was stopped.
NOTE: DATA statement used (Total process time):
real time ** seconds
cpu time ** seconds
*can anyone please suggest what may be the wrong with the code?;
Your statement
tree_ = &trees;
is resolving to
tree_ = '999', '888', '111', '222', '555', '876', '564', '109','456', '543', '897', '009', '321' ;
similarly
hl(i) ne &trees is
hl(i) ne '999', '888', '111', '222', '555', '876', '564', '109','456', '543', '897', '009', '321'
If you don't know why either of those is an error you need to review a lot of basics before attempting to use macro variables.
I have no idea what you are attempting to do with the tree_ = . If you are attempting to use R like syntax to assign values to the array then that is not going to work.
Using instead of the comparison this:
hl(i) not in ( &trees) then
might work if you want to find out that a single value of the array does not appear in the list.
And then
tree = whichc('&trees', of hl(*))>0;
first has a problem because
whichc('&trees' resolves to nothing. it uses the literal value &trees because macro variables do not resolve at all inside single quotes.
If you used whichc("&trees" then the whichc would be looking for the entire value "'999', '888', '111', '222', '555', '876', '564', '109','456', '543', '897', '009', '321' " to be in a single value of the one of the array elements.
You need to describe exactly what you are attempting to do. Best would be to show code that worked before you added in any macro variables. If you don't have such code, then that is the first thing you need to do.
Your statement
tree_ = &trees;
is resolving to
tree_ = '999', '888', '111', '222', '555', '876', '564', '109','456', '543', '897', '009', '321' ;
similarly
hl(i) ne &trees is
hl(i) ne '999', '888', '111', '222', '555', '876', '564', '109','456', '543', '897', '009', '321'
If you don't know why either of those is an error you need to review a lot of basics before attempting to use macro variables.
I have no idea what you are attempting to do with the tree_ = . If you are attempting to use R like syntax to assign values to the array then that is not going to work.
Using instead of the comparison this:
hl(i) not in ( &trees) then
might work if you want to find out that a single value of the array does not appear in the list.
And then
tree = whichc('&trees', of hl(*))>0;
first has a problem because
whichc('&trees' resolves to nothing. it uses the literal value &trees because macro variables do not resolve at all inside single quotes.
If you used whichc("&trees" then the whichc would be looking for the entire value "'999', '888', '111', '222', '555', '876', '564', '109','456', '543', '897', '009', '321' " to be in a single value of the one of the array elements.
You need to describe exactly what you are attempting to do. Best would be to show code that worked before you added in any macro variables. If you don't have such code, then that is the first thing you need to do.
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.