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.
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;
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.
What are you trying to do? Looks like you just want to run PROC FREQ.
proc freq data=sashelp.cars;
tables make;
run;
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.
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?
@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!!
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 ;
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.
thanks every one.
each reply told me a lot.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.