<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: The DATA to DATA Step Macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/The-DATA-to-DATA-Step-Macro/m-p/891928#M352324</link>
    <description>&lt;P&gt;I'm kind of lost about what you are trying to say or do with a .txt file. I'm also lost about why ODS TAGSETS shows up in your code as it is irrelevant to this using macro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What you have to do is take an actual SAS data set, not a .txt file or any other file that is not a SAS data set, and then&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%data2datastep(dsn=&amp;amp;dataSetName, obs=&amp;amp;obsKeep)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;where &amp;amp;datasetname is the name of your SAS data set, let's suppose the data set name is YOUR_DATA_SET_NAME. If you only want the first 27 lines, you set &amp;amp;obskeep equal to 27.&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Example&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%data2datastep(dsn=your_data_set_name, obs=27)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the data is not in a SAS data set yet (for example, it is in a text file), then read it into SAS using PROC IMPORT or via a DATA step. Then you can use the %data2datastep macro on this SAS data set.&lt;/P&gt;</description>
    <pubDate>Thu, 31 Aug 2023 13:30:09 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2023-08-31T13:30:09Z</dc:date>
    <item>
      <title>The DATA to DATA Step Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/The-DATA-to-DATA-Step-Macro/m-p/891922#M352320</link>
      <description>&lt;P&gt;Update. Thanks for&amp;nbsp;the clear and free of jargons explanation.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am not sure if the first Macro in this post contain problems, but columns did not separate. The first Macro also did not work if there are too many columns&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;*data set you want to create demo data for;
%let dataSetName = WORK.QUERY_FOR_AVERAGE;
*number of observations you want to keep;
%let obsKeep = 1;


******************************************************
DO NOT CHANGE ANYTHING BELOW THIS LINE
******************************************************;

%let source_path = https://gist.githubusercontent.com/statgeek/bcc55940dd825a13b9c8ca40a904cba9/raw/865d2cf18f5150b8e887218dde0fc3951d0ff15b/data2datastep.sas;

filename reprex url "&amp;amp;source_path";
%include reprex;
filename reprex;

option linesize=max;
%data2datastep(dsn=&amp;amp;dataSetName, obs=&amp;amp;obsKeep);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;produced the following data sample in the log:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data WORK.QUERY_FOR_AVERAGE;
  infile datalines dsd truncover;
  input File_Ref:$72. _TYPE_:32. _FREQ_:32. avgmarkettop5:32.;
  label PremiumRank="Rank for Variable Premium";
datalines;
Input_100 0 5 14.17
;;;;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;When pasting this into SAS Studio for testing, all data were written into the first column:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="actuarial_0-1693508218597.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/87386iAE20D02D3EA6E6D7/image-size/large?v=v2&amp;amp;px=999" role="button" title="actuarial_0-1693508218597.png" alt="actuarial_0-1693508218597.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;--------------------&lt;/P&gt;&lt;P&gt;There are short articles about how to write data into data step&lt;/P&gt;&lt;P&gt;&lt;A href="https://blogs.sas.com/content/sastraining/2016/03/11/jedi-sas-tricks-data-to-data-step-macro/" target="_blank" rel="noopener"&gt;Jedi SAS Tricks: The DATA to DATA Step Macro - SAS Learning Post&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712" target="_blank" rel="noopener"&gt;How to create a data step version of your data AKA generate sample dat... - SAS Support Communities&lt;/A&gt;&lt;/P&gt;&lt;P&gt;Running the snippet gave warning&amp;nbsp;&lt;/P&gt;&lt;PRE class=""&gt;&lt;CODE&gt;*data set you want to create demo data for;
%let dataSetName = sashelp.Class;
*number of observations you want to keep;
%let obsKeep = 5;


******************************************************
DO NOT CHANGE ANYTHING BELOW THIS LINE
******************************************************;

%let source_path = https://gist.githubusercontent.com/statgeek/bcc55940dd825a13b9c8ca40a904cba9/raw/865d2cf18f5150b8e887218dde0fc3951d0ff15b/data2datastep.sas;

filename reprex url "&amp;amp;source_path";
%include reprex;
filename reprex;

option linesize=max;
%data2datastep(dsn=&amp;amp;dataSetName, obs=&amp;amp;obsKeep);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="actuarial_0-1693486476349.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/87366i82B4A8313601AA54/image-size/large?v=v2&amp;amp;px=999" role="button" title="actuarial_0-1693486476349.png" alt="actuarial_0-1693486476349.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;WARNING: Apparent symbolic reference LBLLIST not resolved.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE class=""&gt;&lt;CODE&gt;filename tagset url "https://gist.githubusercontent.com/statgeek/7be124b98b37d51e7c002b85b6e9cf72/raw/ec0bc5cf8e1a3386c5cf71d2280b242b7f87dadd/SAS_tagsets_sql_reprex.sas";

%include tagset;

   ods tagsets.sql file="/folders/myfolders/class.txt";
   proc print data=sashelp.class ;
   run;
   ods tagsets.sql close;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;Running the above gave errors:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ERROR: Physical file does not exist, E:\folders\myfolders\class.txt.&lt;BR /&gt;ERROR: No body file. TAGSETS.SQL output will not be created.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How to create data steps for forum questions?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If sample data has lots of rows, then how to take a portion?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 31 Aug 2023 19:00:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/The-DATA-to-DATA-Step-Macro/m-p/891922#M352320</guid>
      <dc:creator>actuarial</dc:creator>
      <dc:date>2023-08-31T19:00:55Z</dc:date>
    </item>
    <item>
      <title>Re: The DATA to DATA Step Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/The-DATA-to-DATA-Step-Macro/m-p/891928#M352324</link>
      <description>&lt;P&gt;I'm kind of lost about what you are trying to say or do with a .txt file. I'm also lost about why ODS TAGSETS shows up in your code as it is irrelevant to this using macro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What you have to do is take an actual SAS data set, not a .txt file or any other file that is not a SAS data set, and then&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%data2datastep(dsn=&amp;amp;dataSetName, obs=&amp;amp;obsKeep)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;where &amp;amp;datasetname is the name of your SAS data set, let's suppose the data set name is YOUR_DATA_SET_NAME. If you only want the first 27 lines, you set &amp;amp;obskeep equal to 27.&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Example&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%data2datastep(dsn=your_data_set_name, obs=27)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the data is not in a SAS data set yet (for example, it is in a text file), then read it into SAS using PROC IMPORT or via a DATA step. Then you can use the %data2datastep macro on this SAS data set.&lt;/P&gt;</description>
      <pubDate>Thu, 31 Aug 2023 13:30:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/The-DATA-to-DATA-Step-Macro/m-p/891928#M352324</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-08-31T13:30:09Z</dc:date>
    </item>
    <item>
      <title>Re: The DATA to DATA Step Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/The-DATA-to-DATA-Step-Macro/m-p/891930#M352325</link>
      <description>&lt;P&gt;The first issue is caused by LBLLIST not being defined as one of the LOCAL macro variables.&lt;/P&gt;
&lt;P&gt;You can avoid the note by just creating such a macro variable before calling the macro:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let lbllist=;
%data2datastep(dsn=&amp;amp;dataSetName, obs=&amp;amp;obsKeep);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But that version of the macro also does not seem to generate valid code.&amp;nbsp; Perhaps because the DSD option is not added when the data lines were written? Or because the delmiter is not specified in the generated INFILE statement?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data WORK.CLASS(label='Student Data');
  infile datalines dsd truncover;
  input Name:$8. Sex:$1. Age:32. Height:32. Weight:32.;
datalines;
Alfred M 14 69 112.5
Alice F 13 56.5 84
Barbara F 13 65.3 98
Carol F 14 62.8 102.5
Henry M 14 63.5 102.5
;;;;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Resulting dataset:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1693488708112.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/87367iE34B087FD06D9846/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Tom_0-1693488708112.png" alt="Tom_0-1693488708112.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The second problem is you did not use a valid path in the ODS statement.&amp;nbsp; From your error message your SAS session is running on Windows and the current drive is set to E: and that drive does not have a directory named folders/myfolders under it.&amp;nbsp; &lt;STRONG&gt;Just use a valid filename in the ODS statement.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to generate code you can post to share your data you could also try %DS2POST() macro.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;* Pull macro definition from GITHUB and dump code to the SAS log ;
filename ds2post url
  'https://raw.githubusercontent.com/sasutils/macros/master/ds2post.sas'
;
%include ds2post ;
%ds2post(sashelp.class);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;data work.class (label='Student Data');
  infile datalines dsd dlm='|' truncover;
  input Name :$8. Sex :$1. Age Height Weight ;
datalines4;
Alfred|M|14|69|112.5
Alice|F|13|56.5|84
Barbara|F|13|65.3|98
Carol|F|14|62.8|102.5
Henry|M|14|63.5|102.5
James|M|12|57.3|83
Jane|F|12|59.8|84.5
Janet|F|15|62.5|112.5
Jeffrey|M|13|62.5|84
John|M|12|59|99.5
Joyce|F|11|51.3|50.5
Judy|F|14|64.3|90
Louise|F|12|56.3|77
Mary|F|15|66.5|112
Philip|M|16|72|150
Robert|M|12|64.8|128
Ronald|M|15|67|133
Thomas|M|11|57.5|85
William|M|15|66.5|112
;;;;
&lt;/PRE&gt;
&lt;P&gt;If you want to post just a subset of the data then you could either make a copy of the subset (or make a view that selects the subset).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data males/view=males;
 set sashelp.class;
 where sex='M';
run;
%ds2post(males)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results&lt;/P&gt;
&lt;PRE&gt;data work.males ;
  infile datalines dsd dlm='|' truncover;
  input Name :$8. Sex :$1. Age Height Weight ;
datalines4;
Alfred|M|14|69|112.5
Henry|M|14|63.5|102.5
James|M|12|57.3|83
Jeffrey|M|13|62.5|84
John|M|12|59|99.5
Philip|M|16|72|150
Robert|M|12|64.8|128
Ronald|M|15|67|133
Thomas|M|11|57.5|85
William|M|15|66.5|112
;;;;
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 31 Aug 2023 13:33:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/The-DATA-to-DATA-Step-Macro/m-p/891930#M352325</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-08-31T13:33:18Z</dc:date>
    </item>
    <item>
      <title>Re: The DATA to DATA Step Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/The-DATA-to-DATA-Step-Macro/m-p/891936#M352328</link>
      <description>&lt;P&gt;I forgot what and why I had to change something small in the original macro but attached the version I'm using plus a sample call that creates data step code for the first 10 rows of table sashelp.class.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let source_table=sashelp.class;
%let target_file=c:\temp\class.sas;
%let n_obs=10;
%data2datastep(&amp;amp;source_table,,,&amp;amp;target_file,&amp;amp;n_obs);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 31 Aug 2023 13:57:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/The-DATA-to-DATA-Step-Macro/m-p/891936#M352328</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-08-31T13:57:20Z</dc:date>
    </item>
    <item>
      <title>Re: The DATA to DATA Step Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/The-DATA-to-DATA-Step-Macro/m-p/892043#M352376</link>
      <description>Thank you for the attatched script.&lt;BR /&gt;&lt;BR /&gt;Would you be able to let me know what is "source_table" and "target_file", please?&lt;BR /&gt;&lt;BR /&gt;Does the source need to be a text file, or a SAS table?</description>
      <pubDate>Thu, 31 Aug 2023 19:24:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/The-DATA-to-DATA-Step-Macro/m-p/892043#M352376</guid>
      <dc:creator>actuarial</dc:creator>
      <dc:date>2023-08-31T19:24:31Z</dc:date>
    </item>
    <item>
      <title>Re: The DATA to DATA Step Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/The-DATA-to-DATA-Step-Macro/m-p/892055#M352384</link>
      <description>&lt;P&gt;This method is great! Thank you so much.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I was able to generate the data step, but I don't follow how to filter to keep a sample of required information.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I got a snippet here:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data work.QUERY_FOR_AVERAGE1_0000 ;&lt;BR /&gt;infile datalines dsd dlm='|' truncover;&lt;BR /&gt;input File_Ref :$72. _TYPE_ _FREQ_ avg ;&lt;BR /&gt;format File_Ref $char72. ;&lt;BR /&gt;informat File_Ref $char72. ;&lt;BR /&gt;datalines4;&lt;BR /&gt;Input_100|0|85|37.2225882352941&lt;BR /&gt;Input_101|0|82|51.7714634146341&lt;BR /&gt;Input_102|0|64|23.78859375&lt;BR /&gt;Input_103|0|82|34.3660975609756&lt;BR /&gt;Input_104|0|85|32.2994117647058&lt;BR /&gt;Input_105|0|85|36.7142352941176&lt;BR /&gt;Input_106|0|82|61.3445121951219&lt;BR /&gt;Input_107|0|85|19.6250588235294&lt;BR /&gt;Input_108|0|85|28.0389411764705&lt;BR /&gt;Input_109|0|67|42.8680597014925&lt;BR /&gt;Input_110|0|85|40.6709411764705&lt;BR /&gt;Input_112|0|85|23.7874117647058&lt;BR /&gt;Input_113|0|85|34.6115294117647&lt;BR /&gt;Input_114|0|64|52.91453125&lt;BR /&gt;3 The SAS System 12:30 Thursday, August 31, 2023&lt;/P&gt;&lt;P&gt;Input_115|0|84|51.5966666666666&lt;BR /&gt;Input_116|0|81|67.6408641975308&lt;BR /&gt;Input_117|0|84|25.7155952380952&lt;BR /&gt;Input_118|0|84|40.9023809523809&lt;BR /&gt;Input_119|0|84|39.5719047619047&lt;BR /&gt;Input_30|0|73|95.3902739726027&lt;BR /&gt;;;;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How do I keep, say, the first 5 rows?&lt;/P&gt;</description>
      <pubDate>Thu, 31 Aug 2023 19:41:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/The-DATA-to-DATA-Step-Macro/m-p/892055#M352384</guid>
      <dc:creator>actuarial</dc:creator>
      <dc:date>2023-08-31T19:41:22Z</dc:date>
    </item>
    <item>
      <title>Re: The DATA to DATA Step Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/The-DATA-to-DATA-Step-Macro/m-p/892057#M352386</link>
      <description>I can manually delete rows.&lt;BR /&gt;&lt;BR /&gt;I don't follow the part&lt;BR /&gt;&lt;BR /&gt;data males/view=males;&lt;BR /&gt;set sashelp.class;&lt;BR /&gt;where sex='M';&lt;BR /&gt;run;&lt;BR /&gt;%ds2post(males)&lt;BR /&gt;&lt;BR /&gt;What does "males/view=males" mean?</description>
      <pubDate>Thu, 31 Aug 2023 19:45:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/The-DATA-to-DATA-Step-Macro/m-p/892057#M352386</guid>
      <dc:creator>actuarial</dc:creator>
      <dc:date>2023-08-31T19:45:59Z</dc:date>
    </item>
    <item>
      <title>Re: The DATA to DATA Step Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/The-DATA-to-DATA-Step-Macro/m-p/892140#M352390</link>
      <description>&lt;P&gt;The /VIEW= option on the DATA statement allows you to create a VIEW instead of a physical dataset.&amp;nbsp; The name after = has to match one of the names listed on the data statement.&lt;/P&gt;</description>
      <pubDate>Thu, 31 Aug 2023 20:09:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/The-DATA-to-DATA-Step-Macro/m-p/892140#M352390</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-08-31T20:09:29Z</dc:date>
    </item>
  </channel>
</rss>

