BookmarkSubscribeRSS Feed
satish123
Fluorite | Level 6

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.

9 REPLIES 9
Kurt_Bremser
Super User

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;
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

Tom
Super User Tom
Super User

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

 

proc freq data=sashelp.cars;
  tables make;
run;
Astounding
PROC Star

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.

satish123
Fluorite | Level 6

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?

 

Astounding
PROC Star

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 ;

 

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.  

satish123
Fluorite | Level 6

thanks every one.

 

each reply told me a lot.

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 9 replies
  • 1310 views
  • 2 likes
  • 5 in conversation