


With the data2datasetp macro in my toolbox, I can easily post a little snip of data to go with my code whenever I'm asking for help in a forum.Īs usual, you can download a zip file containing the complete SAS program and a copy of this article from the link below. If there is sensitive information included, at this point I can easily anonymize it by editing the clear text values in the DATALINES portion of the DATA step. I can edit the code to change the LIBREF of the dataset to be produced to WORK. Now let's test that baby on some real data! I want to produce a DATA step that will create 5 rows of data from the SASHELP.CARS dataset. NOTE: DATA2DATASTEP cannot be used in-line - it generates code. Obs: Max observations to include the created dataset. Optional.ĭefault is create_&lib._&dsn._data.sas in the SAS default directory. Optional.įile: Filename for the DATA step produced. Purpose: Converts a data set to a SAS DATA step.ĭsn: Name of the dataset to be converted. Here is the SQL query that gets a space delimited list of the variable names which we will use to produce values for the DATALINES portion of our DATA step:Īfter the macro is compiled, %data2datastep(!help) produces this information in the SAS log: Calling the macro with !help as the value of the first parameter - like this: %data2datastep(!help) - will get you syntax help in the SAS log.Ī little about how the macro works: after all of the parameter checking is complete, the macro uses some SQL to gather information about the data set and put that information into macro variables for later use. The macro name is data2datastep, and like most of the macros I write for my own use, it is self-documenting. I put this macro program in my SAS AUTOCALL path so it's always available when I need it. I can then share the the code that produces the input data set as part of the program when I ask for help. I solved this problem for myself a long time ago by writing a macro that takes a SAS data set and creates a DATA step which uses DATALINES to reproduce the data using code. So how can I turn a SAS DATA set into a DATA step? Why, write an DATA set to DATA step macro, of course! all make a difference, so often you can't just attach a dataset, even if the forum accepts attachments - it wouldn't be usable. It's easy to share your code on a forum with a quick copy and paste, but it's not so easy to share data. This makes it easy to run their code on your own SAS session so you can help pinpoint the problem more quickly. I was answering questions about SAS in a forum the other day, and it struck me how much easier it is to help folks if they can provide a snippet of data to go along with their program when asking others to help troubleshoot.
