Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Basic SAS question

Status
Not open for further replies.

evaaseow

Programmer
Jan 25, 2007
29
CA
Please correct me if I am wrong, data steps/proc statements are unique to SAS, Macros are equivalent to functions in other languages.

Now, besides surrounding code statements with data steps/proc/macro is there a way of just coding if statements and other functions in SAS?
 
My logical thinking comes from programming in VB, so please bear with me.

Is this possible?

data _null_;
if x=0 then;
proc sql;
sql statement
quit;
end;
run;
 
No not possible. DATA steps and PROC steps can't be mixed. In fact, SAS will treat the PROC statement as an end for the DATA statement (equiv. -->RUN)

Macro is not equiv. to functions in other languages. MACRO code is more like perl's text substition. All the MACRO code does is resolve to valid SAS code at time of execution. In other words, at runtime SAS will have the execution process wait to resolve the MACRO code and then execute the full SAS program. While in other languages (C, C#, Java) the function or methods are self contained runable code objects.

The trick that I learned when dealing with and SAS MACRO is what does that macro resolve at runtime? Is the code that's generated at run-time valid? You can see what the code will resolve to by inserting the following statement at the top of your code.
Code:
OPTIONS symbolgen macrogen mprint;


Now to get on with your question. I think that you mean to limit the PROC SQL from execution depending on your code. You can do this with macro flags. These are macro variables that can be set in your program and your code can take advantage of their values to either execute of stop the current statement.
If you have a specific task let me know I can probably get you an example.
I hope that this helps you,
Klaz
 
Thanks for the clarification.

Here is one scenario:

I wish to insert data into a table but based on the date.

For example(this is general logic, not code specific)

if date = jan then
insert data into table 1
elseif date = feb
insert data into table 2
end if
 
In general, you build the table of data that you want in a temp source table and than you can append or insert the data to your target table. If your target table exists with the appropiate records, you use the MERGE statement in a DATA step (this is almost like the INSERT statement in SQL). If the target's records do not exist you can append the data via the PROC APPEND step (almost like a UNION statement in SQL).

I hope that this has helped you.
Klaz
 
You can do conditional execution of data and proc steps using macro code, but these can only be run inside macros, like this:-
Code:
* Define macro which I'm naming MYMAC *;
%macro MYMAC();
  %if "&DATE" = "JAN" %THEN
  %DO;
     proc sql;
     ...
     ...
     quit;
  %END;
%MEND MYMAC;

* Run macro MYMAC *;
%MYMAC;

Chris
Business Analyst, Code Monkey, Data Wrangler.
SAS Guru.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top