I created a macro variable &count which equals 3 in this case, and want to use it in another proc sql, but it is not working, please suggest, thanks.
proc sql noprint;
select count(distinct a) into: count from A;
quit;
%global &count.;
proc sql noprint;
select distinct b into : grp1 - :grp&count. from B;
quit;
Never mind, I have figured it out, need to remove the lead space in &count
proc sql noprint;
select left(put(count(distinct a), 8.)) into: count from A;
quit;
proc sql noprint;
select distinct b into : grp1 - :grp&count. from B;
quit;
Never mind, I have figured it out, need to remove the lead space in &count
proc sql noprint;
select left(put(count(distinct a), 8.)) into: count from A;
quit;
proc sql noprint;
select distinct b into : grp1 - :grp&count. from B;
quit;
@kc_sas wrote:
Never mind, I have figured it out, need to remove the lead space in &count
proc sql noprint;
select left(put(count(distinct a), 8.)) into: count from A;
quit;
proc sql noprint;select distinct b into : grp1 - :grp&count. from B;
quit;
Congratulations on figuring this out. Note: any conversion of numeric to character, and all macro variables are character, means you have to take control of the conversion or get results that may not work as intended.
Alternatively use the -L option with the Put function to left justify output.
put (variable, format. -L)
You don't need to specify the ending of a list when creating macro variables with SQL.
proc sql noprint; select distinct b into : grp1 - from B; quit;
This will create the number of macro variables required automatically.
proc sql;
select distinct age into :age1-
from sashelp.class;
quit;
%put &sqlobs;
*if you need it for future loops;
%let nobs = &sqlobs;
*check macro variables created;
%put &age1.;
*age6 = 16;
%put &&age&sqlobs.;
the outcome in this case would be:
&grp1 = b1
&grp2 = b2
&grp3 = b3
because the value of &count is changing, so need to do it in a robust way
@kc_sas wrote:
the outcome in this case would be:
&grp1 = b1
&grp2 = b2
&grp3 = b3
because the value of &count is changing, so need to do it in a robust way
You are working WAY TOO HARD and making SQL work too hard also.
Let SQL do the counting for you.
proc sql noprint;
select distinct b
into :grp1 -
from B
;
%let count=&sqlobs;
quit;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.