I struggle to understand the behavior of the following code (SAS 9.3). (Although the objective is to count distinct values for the columns in "Sashelp.Cars", i am primarily interested in help with understanding why it is not working as expected.) data testing (keep = libname memname name dist_count colname1 colname2);
set sashelp.vcolumn(obs = 3);
where libname = 'SASHELP' and memname = 'CARS';
call symput('colname1', name);
rc = dosubl("
%let colname2 = &colname1;
proc sql noprint;
select
count(distinct &colname1) into :dist_count
from
SASHELP.CARS quit;
");
dist_count = symget('dist_count');
colname1 = symget('colname1');
colname2 = symget('colname2');
run;
%put &colname1;
%put &colname2; The first time the code is submitted it gives the following warning: WARNING: Apparent symbolic reference COLNAME1 not resolved. The last two lines both outputs "Type" which is what I expected. In the resulting dataset, the column "dist_count" and "colnam1" is as expected. "colname2" on the other hand takes the value "&colname1". When the code is submited one more time, no warnings are generated but in the dataset the column "dist_count" are no longer as expected. Also all values in "colname2" are now "Type". Again, the last two lines generates "Type". The most obvious problem is that "dist_count" no longer is assigned the right values the second time the code is submited. Im interessted in knowing why is this is, and how is it is to be avoided in other scenarios?
... View more