Desktop productivity for business analysts and programmers

Accepted Solution

proc sql send result count in macro

Reply
Occasional Contributor
Posts: 16

Re: proc sql send result count in macro

Hello ,

 

@

 

@KurtBremser

 

Yes I have read several documentations about SAS macro in PDF 130-30 , 243-29 , base_macro_6997 , p071-27 .

 

I want that the macro count the number of line in a table. Why I use a macro , because to develop my SAS program in the futur I want use several time this macro , for several count in 4 or 6 tables.

 

if you want give an opinion on the SAS code , my actual code is this :

 

%macro test(var=);

count(&var);

%mend;

data work.temp;
set mybdd.mytable;
call symput(myfield);

run ;

 

Super User
Posts: 7,431

Re: proc sql send result count in macro

If you had read the macro documentation, you would know how to use call symput, as it is part of the macro/data step interface.

Your code has obvious errors (I gave you the reason in my previous post), and it won't work.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 7,431

Re: proc sql send result count in macro


azertyuiop wrote:

I have tryed this macro :

 

%macro test(var=);

count(&var);

%mend;

data work.temp;
set mybdd.mytable;
call symput(myfield);

run ;

There is this message of error in logs :

 

ERROR 252-185: The SYMPUT subroutine call does not have enough arguments.

You obviously have not read the documentation. Do so to avoid such extremely simple mistakes. The documentation is there for a reason, not consulting it is a sign of foolishness.

call symput() requires TWO arguments.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 5,365

Re: proc sql send result count in macro

As you can see from all the questions here, your code is far from working.  I will give you something that works (I think), but you still have a lot of studying to do to understand why this code works and your code doesn't.  (At least I'm assuming this code works.  It might need minor tweaking, since I can't test it.)

 

%macro countrecs (dsn=);

   proc sql noprint;

   create table counts as select count(*) as record_count from &dsn;

   quit;

   data _null_;

      set counts;

      call symputx('nb_line', record_count, 'G');

   run;

%mend countrecs;

 

Then you could test it with:

 

%countrecs (dsn=database.original_table)

%put Number of lines:  &nb_line;

 

Note that I removed ID from the code.  Presumably, you would like your code to run even if the incoming data does not contain a field named ID.

Super User
Posts: 11,134

Re: proc sql send result count in macro

Please Provide some example input data and an example of what the final result should be. Posting non-working code does not describe the actual issue or result.

 

There are so many ways to count things in SAS and then combine that result with something else that choices need to be made based on something concrete. For instance if this is involved is displaying a result a human reads such as a report one way may be more appropriate than if the value is to be added to a data set and used for computation.

Solution
a week ago
Occasional Contributor
Posts: 16

Re: proc sql send result count in macro

Hello ,

 

I have used the SAS code for the macro . It's a excellent code . I can obtain the result of count in the log and in a table Man Wink

 

options compress=yes sortsize=max;

/* call database */

libname b8x meta library="my_data_base_john_doe";

/* macro to count lines */

%macro countrecs (dsn=);

   proc sql noprint;

   create table counts as 
   
   select count(*) as record_count 
   
   from &dsn;

   quit;

   data _null_;

      set counts;

      call symputx('nb_line', record_count, 'G');

   run;

%mend countrecs;

proc sql inobs=2500;

/* see several information on table */

create table consultation_abcd as select * from b8x.abcd; 
create table consultation_efgh as select * from b8x.efgh;

run; 

%countrecs (dsn=b8x.AB4D)

%put Number of lines: &nb_line;

Inside this SAS code I have had my proc sql.

 

Thanks for you help Smiley Wink

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 20 replies
  • 336 views
  • 12 likes
  • 7 in conversation