SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
kc_sas
Fluorite | Level 6

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;

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
kc_sas
Fluorite | Level 6

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;

View solution in original post

6 REPLIES 6
kc_sas
Fluorite | Level 6

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;

ballardw
Super User

@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) 
Reeza
Super User

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.;
fja
Lapis Lazuli | Level 10 fja
Lapis Lazuli | Level 10
What is the intended outcome of the latter sql ?
--fja
kc_sas
Fluorite | Level 6

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

Tom
Super User Tom
Super User

@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;

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

Register now!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1803 views
  • 3 likes
  • 5 in conversation