How do you know which data set is the latest? Is there some naming convention like a date in the name or a sequence number?
If so then you don't need to write a macro but you query the dictionary.tables and populate a macro variable for later use (see sample code below).
proc sql;
select cats(libname,'.',memname) into :my_table_name
from dictionary.tables
where libname='COMMON' and memname ....<some condition(s) selecting the required table>
;
quit;
data want;
set &my_table_name;
run;
As mentioned by @Patrick, DICTIONARY.TABLES should contain the information you need. I suspect that you could use the CRDATE and MODATE columns. To get the latest, use the max() function with a HAVING statement.
How is this different than your previous question which had several worked examples?
If you absolutely want a macro because it's your homework wrap the code between %macro and %mend.
%macro latest_date();
*code already provided;
%mend;
%latest_date;
If you want some sort of dynamic nature to this you need to be more explicit in your requirements. You could add parameters for library name and what you wanted the macro variable to be called. But you haven't explained what you want.
Here's a link on into to macro programming to help you change the code if required.
http://www.ats.ucla.edu/stat/sas/seminars/sas_macros_introduction/
@Ravikumarpa4 wrote:
Just I wanted to extract latest dataset in a library called common. I tried to use proc sql but it's display all the datasets, I need single dataset which id created recently and later I need to write macro for the same.
Any idea how to do it?
See this example:
16 proc sql noprint; 17 select name into :names from sashelp.class order by name descending; 18 quit; NOTE: PROCEDURE SQL used (Total process time): real time 0.01 seconds cpu time 0.01 seconds 19 %put "names=&names"; "names=William "
Note that the macro variable contains only one entry, coming from the first line that the order by clause delivers.
So
proc sql noprint;
select memname into :memname from dictionary.tables
where libname = 'COMMON'
order by memname descending;
quit;
should deliver the last dataset from library COMMON in macro variable &memname.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.