DATA Step, Macro, Functions and more

Exporting a Dataset to Datalines

Accepted Solution Solved
Reply
Super Contributor
Posts: 480
Accepted Solution

Exporting a Dataset to Datalines

Is there a good article on how to export a SAS dataset you have in a stored process to a datalines statement.  I want to take a dataset I have in a stored process and share it in a forum post.


Accepted Solutions
Solution
‎04-06-2016 01:18 PM
Super User
Posts: 17,823

Re: Exporting a Dataset to Datalines


All Replies
Super User
Posts: 17,823

Re: Exporting a Dataset to Datalines

Why yes there is 😀 

 

http://blogs.sas.com/content/sastraining/2016/03/11/jedi-sas-tricks-data-to-data-step-macro/

 

Also, if you're on EG there's a custom task that can be used - see Chris Hemedingers blog. 

 

 

Super Contributor
Posts: 480

Re: Exporting a Dataset to Datalines

Which post is it in his blog?

 

General blog link:

http://blogs.sas.com/content/sasdummy/author/chrishemedinger/page/4/

Solution
‎04-06-2016 01:18 PM
Super User
Posts: 17,823

Re: Exporting a Dataset to Datalines

Super Contributor
Posts: 480

Re: Exporting a Dataset to Datalines

[ Edited ]

Reeza,

 

When I tried to run the macro in the blog http://blogs.sas.com/content/sastraining/2016/03/11/jedi-sas-tricks-data-to-data-step-macro/ I was unable to get it to work. How do you use this Marco?  I copied it to Enterprise Guide and tried passing parameters.

Super User
Posts: 17,823

Re: Exporting a Dataset to Datalines

I wrote a post about how to do it using SAS UE

https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat...

 

I'm assuming you have a dataset somewhere that you'd like to create the dataset for?

 

You would open the data2datastep.sas macro and run that.

Then call the macro, The third parameter is the name and path of the file that gets created. You'll have to specify that for your system. 

Something like the following I imagine:

 

%data2datastep(cars,sashelp,C:\temp\out.sas,5)

 

It probably depends a bit on your set up, if you're on a server for instance it might be different. 

Super Contributor
Posts: 480

Re: Exporting a Dataset to Datalines

I've got a server setup:

 

ERROR: Insufficient authorization to access /sasconfig/Lev1/SASApp/C:\temp\out.sas.

Super User
Posts: 17,823

Re: Exporting a Dataset to Datalines

Yeah, unfortunately you'll need a place to be able to create the file on the server. What happens if you leave the third parameter blank?

 

I'll take a look at the macro later and see if there's a way to output it to a dataset or log that could then be copied to avoid this issue. 

 

I've only perused the code so far, so not sure if that's even possible.

 

I assume you weren't able to install the add in?

Super Contributor
Posts: 480

Re: Exporting a Dataset to Datalines

[ Edited ]

I found a location to save the output to that works for me.

 

Is the add on safe?  It looks like an install file from an unknown vendor.

 

I’m trying to modify the code to allow me to output an active dataset so that I can narrow down the dataset before outputting it.  But understanding the code is tricky.  

 

If I could replace this section:

 

from dictionary.columns
where libname="&lib"
and memname="&dsn"

 

with something like:

 

from work.mytable

Super User
Posts: 17,823

Re: Exporting a Dataset to Datalines

What do you mean by narrow down the dataset?

Wouldn't you subset the table and then pass that to the macro?

 

data sub;

set have;

where <limit>;

run;

 

%data2datastep(sub, work, ,20);

 

The part of the code you're currently viewing would provide the column names and data types. I'm not sure that referencing your table would make sense. 

 

The add on is safe, it's published by SAS, @ChrisHemedinger

I found that I needed IT Admin rights to unlock it though.

 

 

Super Contributor
Posts: 480

Re: Exporting a Dataset to Datalines

Reeza, 

 

That works.  I wasn't sure how to reference the work library like that. 

 

Thanks for your help on this.

Super Contributor
Posts: 480

Re: Exporting a Dataset to Datalines

[ Edited ]

I put 10000 in for obs but my output is cut off at about 2000 rows.  My dataset only has 4 columns so its not huge.

Super User
Posts: 17,823

Re: Exporting a Dataset to Datalines

There's no limit in the code that I can see that would restrict it to 2000 records...when you run the code, it only creates 2000 observations?

Super Contributor
Posts: 480

Re: Exporting a Dataset to Datalines

It creates 2321 records and cuts off on the last record.  I think it is some sort of length issue. 

 

I'm looking at how to install the plugin.

Super User
Posts: 17,823

Re: Exporting a Dataset to Datalines

This works for me. Does it truncate for you as well?

 

data random;
do i=1 to 10000;
x=1;
y=x*2;
z=rand('normal', 25, 5);
w=floor(y*z);
output;
end;
run;


%data2datastep(random,work,C:\_localdata\out.sas,10000)
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 16 replies
  • 544 views
  • 5 likes
  • 2 in conversation