- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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)
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
--fja
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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;