Desktop productivity for business analysts and programmers

Sequential display using macro variables in log window

Reply
Contributor
Posts: 26

Sequential display using macro variables in log window

Hi,

    Its been so long,

    here is the macro that i've used.

 

            data _null_;
                set sashelp.cars end=eof;
                     by make;
                     retain mk 0;
                          if first.make then do;
                          mk=mk+1;
                              call symput('Mfg'||strip(put(mk,3.)),make);
                          end;
                          if eof then do;
                              call symput('mfg_no',strip(put(mk,3.)));
                          end;
            run;

 

            %macro sat(count);
                %do i=1 %to &count;
                      Manufacturer&i is &&mfg&i;
                %end;
            %mend sat;

            %put number of mfgs are &mfg_no;
            %put %sat(&mfg_no);

 

the code suppose to display for all 'i' in SAS log, but its not working some how.

if possible how to save these in a sas dataset.

 

Thanks in advance,

Satish.A.

Esteemed Advisor
Posts: 6,702

Re: Sequential display using macro variables in log window

a) You can use the put statement directly in the data step to write your values to the log

b) from what I see, you're just missing a %put in the %do loop:

%put Manufacturer&i is &&mfg&i;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Esteemed Advisor
Esteemed Advisor
Posts: 7,251

Re: Sequential display using macro variables in log window

Sorry, I am really not seeing the point in this code, what is it your trying to do?  Is it just the unique makes and count if so:

proc sort data=have out=want nodupkey;
  by make;
run;
data _null_;
  set want nobs=n;
  length string $2000;
  string=cats("N=",put(_n_,best.),", Make=",make,", Total Obs=",put(n,best.));
  put string;
run;

No need to make a macro hill out of a molehill.

Super User
Super User
Posts: 6,373

Re: Sequential display using macro variables in log window

What are you trying to do?  Looks like you just want to run PROC FREQ.

 

proc freq data=sashelp.cars;
  tables make;
run;
Respected Advisor
Posts: 4,998

Re: Sequential display using macro variables in log window

While you've received valid comments so far, here's another variation on the theme:

 

  data _null_;
         set sashelp.cars;
         by make;
              if first.make;
              mk=mk+1;

              put 'Manufacturer ' mk 'is ' make;
run;

 

You can turn this into a macro problem for the practice, but it's not really an application that requires any macro language.

Contributor
Posts: 26

Re: Sequential display using macro variables in log window

yes, it was my practise on macros.

 

thanks for your sugesion and code that made this possible.

 

is there any way that my macro can do the same? and save in a dataset?

 

Esteemed Advisor
Posts: 6,702

Re: Sequential display using macro variables in log window


satish123 wrote:

 

is there any way that my macro can do the same? and save in a dataset?

 


By $DEITY, no!!

MACRO IS FOR GENERATING CODE, NOT DATA!!

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Respected Advisor
Posts: 4,998

Re: Sequential display using macro variables in log window

Yes, that's true.  Macro language does not create a data set.  The best it can do is generate the SAS code that would be needed to create a data set.

 

You could probably get your macro to work by removing a semicolon.  It would still be complex code, and I'm not sure if you could visualize why this would make a difference.  But the offending semicolon is inside the %DO loop:

 

Manufacturer&i is &&mfg&i ;

 

 

Esteemed Advisor
Esteemed Advisor
Posts: 7,251

Re: Sequential display using macro variables in log window

Practice lesson 1 would be knowing where to use macro code.  Macro itself does nothing, it is merely a tool to generate some text.  That text is generally valid Base SAS code which is then compiled and executed.  Therefore the main part to learn is Base SAS which is the bit that does the work.  

Contributor
Posts: 26

Re: Sequential display using macro variables in log window

thanks every one.

 

each reply told me a lot.

 

Ask a Question
Discussion stats
  • 9 replies
  • 284 views
  • 2 likes
  • 5 in conversation