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.
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.
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.