BookmarkSubscribeRSS Feed
gupta_shubham
Obsidian | Level 7

How to run nested data step in sas or

how to call a data step in another data step ?

5 REPLIES 5
Amir
PROC Star

Hi,

A data step is boundary is met when another data statement is encountered and so data steps cannot be nested.

Regards,

Amir.

Vince28_Statcan
Quartz | Level 8

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

dakum_paul
Calcite | Level 5

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;

 

 

JackHamilton
Lapis Lazuli | Level 10

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.

 

 

JackHamilton
Lapis Lazuli | Level 10

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

 

 

 

SAS Innovate 2025: Call for Content

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!

Submit your idea!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 4593 views
  • 0 likes
  • 5 in conversation