How to run nested data step in sas or
how to call a data step in another data step ?
Hi,
A data step is boundary is met when another data statement is encountered and so data steps cannot be nested.
Regards,
Amir.
It is actually arguably possible to nest datastep with SAS9.2 but it requires tedious efforts.
Using PROC FCMP, you can create a routine that you can call in your outer data step.
With the RUN_MACRO and dosubl tools in proc FCMP, you can force SAS to execute the inner data step that you would preemptively have wrapped in a macro. The RUN_MACRO allows you to execute the macro right away rather than piling it up to be executed at the end of your current data step like with call execute.
Typically, there would be other better ways around so if you are not an experienced SAS user, you would probably be better detailing the intention behind your code as there are very likely ways around that. However, if you are in for a nice self-learning experience, look into
http://support.sas.com/resources/papers/proceedings12/227-2012.pdf
Cheers
Vince
I don't know of a way to creating a nested data step but you can use PROC SQL and then create nested select statements. Something like below
PROC SQL; CREATE TABLE want AS ( SELECT * FROM have1 WHERE var1 IN ( SELECT var1 FROM have2 WHERE cond1 = 'THING' ) ); QUIT;
No need for FCMP. You can put code into a string and then call it with DOSUBL inside a data step.
data _null_;
rc = dosubl('proc format; value something 1=1; run;');
rc = dosubl('proc format; value somethingelse 2=2; run;');
run;
You can see the SAS log for the executed code, but I don't know if it's possible to see the source lines as well.
There are several SAS Global Forum papers on dosubl:
https://www.lexjansen.com/search/search_sgf.php?q=dosubl
Start with papers by Rick Langston and Mike Rhoads, in addition to the paper by Jason Secosky that has already been mentioned.
https://support.sas.com/resources/papers/proceedings13/032-2013.pdf
https://support.sas.com/resources/papers/proceedings12/004-2012.pdf
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.