Help using Base SAS procedures

How to write macro to extract first obs from particular library

Reply
Contributor
Posts: 39

How to write macro to extract first obs from particular library

Hi how to write a macro to extract first obs from library which is shorted by crdate if first obs is blank then its need to extract second obs so on... Kindly help me
Super User
Posts: 17,836

Re: How to write macro to extract first obs from particular library

When you need to write a macro you first need working code and then you convert it. 

Do you have working code that needs to be turned into a macro? And since this is a trivial task why do you need a macro? Which parts are dynamic in nature. Please be more explicit in your questions. 

Contributor
Posts: 39

Re: How to write macro to extract first obs from particular library

Hi

Yes I've working code, whenever I run the working code it'll generate new
dataset here I need only that dataset some times if data is not available
old dataset will be there. So I need write a macro so that it can extractt
first obs dataset.

Thanks and Regards
Ravikumar

##- Please type your reply above this line. Simple formatting, no
attachments. -##
Super User
Posts: 6,939

Re: How to write macro to extract first obs from particular library

Post the code and describe which part of it should be parameterized.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 39

Re: How to write macro to extract first obs from particular library

Hi
Code which I wrote is
Proc sql;
Create table abc as
Select * from
Dictionary.tables
Where libname= 'common'
Order by crdate desc;
Quit;

When I'm running above code its shows all the datasets in that library but
I want only one data set which is created most recently by writing macro.
Thanks

##- Please type your reply above this line. Simple formatting, no
attachments. -##
Super User
Posts: 6,939

Re: How to write macro to extract first obs from particular library

Use the having clause and the max() summary function in SQL:

proc sql;
select * from dictionary.tables
where libname = 'common'
having crdate = max(crdate);
quit;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 39

Re: How to write macro to extract first obs from particular library

Hi

Now coding is working fine but I need assign macro how to do that,?

##- Please type your reply above this line. Simple formatting, no
attachments. -##
Super User
Posts: 6,939

Re: How to write macro to extract first obs from particular library

Which parts of the code need to be parameterized?

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 39

Re: How to write macro to extract first obs from particular library

Hi

crdate part

##- Please type your reply above this line. Simple formatting, no
attachments. -##
Super User
Posts: 6,939

Re: How to write macro to extract first obs from particular library

Do you want to change the variable that's used, or do you want to compare with a specific value?

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 39

Re: How to write macro to extract first obs from particular library

Hi
variable will be same but value will change

##- Please type your reply above this line. Simple formatting, no
attachments. -##
Super User
Posts: 17,836

Re: How to write macro to extract first obs from particular library

But the max crdate is always used. If the variable name doesn't change that doesn't need to be parametrized. 

 

Post a few different versions of what the code would be for us to understand what you need a macro for. 

Contributor
Posts: 39

Re: How to write macro to extract first obs from particular library

Hi

Now I need to assign macro to 'memname' variable then how can I do.

Thanks

##- Please type your reply above this line. Simple formatting, no
attachments. -##
Super User
Posts: 6,939

Re: How to write macro to extract first obs from particular library

%macro your_macro_name(memname);

*here comes your code;
*example;
where memname = "&memname";

*further code;
%mend;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Super User
Posts: 7,401

Re: How to write macro to extract first obs from particular library

Post code, and examples to clarify the question.  As for your problem, why do you need a macro?

/* Test data */
proc sql; create table a (a char(2)); quit; data b; b=1; run; /* This is the code */ proc sort data=sashelp.vtable out=inter; by memname; where libname="WORK" and nobs > 0; run; data _null_; set inter (obs=1); call execute(cats('data want; set work.',memname,' (obs=1); run;')); run;
Ask a Question
Discussion stats
  • 15 replies
  • 363 views
  • 2 likes
  • 4 in conversation