<?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: Replacing values and outputting data as separate csv file in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Replacing-values-and-outputting-data-as-separate-csv-file/m-p/805770#M317407</link>
    <description>&lt;P&gt;Ah nevermind, I figured it out by writing a macro, thanks again!&lt;/P&gt;</description>
    <pubDate>Mon, 04 Apr 2022 02:43:11 GMT</pubDate>
    <dc:creator>elbarto</dc:creator>
    <dc:date>2022-04-04T02:43:11Z</dc:date>
    <item>
      <title>Replacing values and outputting data as separate csv file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-values-and-outputting-data-as-separate-csv-file/m-p/805756#M317399</link>
      <description>&lt;P&gt;I have a dataset called low_2002 as follows:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;DATA low_2002;
input county_id	ammonia nitrogen voc;
DATALINES;
1001	568.76	2329.47	6695.85
1003	0.24	0.00	50.41
1005	10.73	347.76	325.23
1007	0.00	59.06	77.60
1009	4.99	0.00	0.00
;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;and another dataset called TRI as follows:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;DATA TRI;
input county_id	voc;
DATALINES;
1001	432.47
1003	0.00
1005	3.22
1007	0.00
1009	1.52
;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;What I would like to do is replace each county_id's VOC value in low_2002 with the corresponding value in TRI, sequentially, while keeping everything else exactly the same. This will produce 5 datasets. I want to name them low_2002_TRI_1, low_2002_TRI_2, ..., low_2002_TRI_5. For example, low_2002_TRI_1 will be: (county_id 1001 has the VOC value of &lt;CODE class=""&gt;6695.85&lt;/CODE&gt; replaced with &lt;CODE class=""&gt;432.47&lt;/CODE&gt;, while everything else stays the same).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;DATA low_2002_TRI_1;
input county_id	ammonia nitrogen voc;
DATALINES;
1001	568.76	2329.47	432.47
1003	0.24	0.00	50.41
1005	10.73	347.76	325.23
1007	0.00	59.06	77.60
1009	4.99	0.00	0.00
;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;low_2002_TRI_2 will be:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;DATA low_2002_TRI_2;
input county_id	ammonia nitrogen voc;
DATALINES;
1001	568.76	2329.47	6695.85
1003	0.24	0.00	0.00
1005	10.73	347.76	325.23
1007	0.00	59.06	77.60
1009	4.99	0.00	0.00
;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;and all the way until low_2002_TRI_5:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;DATA low_2002_TRI_5;
input county_id	ammonia nitrogen voc;
DATALINES;
1001	568.76	2329.47	6695.85
1003	0.24	0.00	50.41
1005	10.73	347.76	325.23
1007	0.00	59.06	77.60
1009	4.99	0.00	1.52
;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Then, I would like to save each of the low_2002_TRI_1, low_2002_TRI_2, ..., low_2002_TRI_5 as csv files with no variable names and the county_id variable dropped, so that low_2002_TRI_1.csv looks like:&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;568.76&lt;/TD&gt;&lt;T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/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The above is just an example of 5 county_id's, but in reality, I will be applying this procedure to a fuller dataset with approximately 3000 county_id's.&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;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 03 Apr 2022 23:00:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-values-and-outputting-data-as-separate-csv-file/m-p/805756#M317399</guid>
      <dc:creator>elbarto</dc:creator>
      <dc:date>2022-04-03T23:00:53Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing values and outputting data as separate csv file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-values-and-outputting-data-as-separate-csv-file/m-p/805760#M317403</link>
      <description>&lt;P&gt;At first you might imagine this task would read one obs from LOW_2002 at a time.&amp;nbsp; It would then selectively output the unchanged obs to all but one of the output datasets.&amp;nbsp; Then make the change, using the data from TRI, and output the changed obs to the single appropriate dataset.&amp;nbsp; Go on and do the next record from LOW_2002.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But this requires a lot of hard coding of dataset names, among other complexities.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A better way is to load the original LOW_2002 into a hash object (i.e. a table in memory).&amp;nbsp; Then read one record at a time from TRI.&amp;nbsp; For each such record from TRI, modify the variable VOC in the hash object and then output the hash object to the corresponding dataset.&amp;nbsp; Then restore the original value to the hash object.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA low_2002;
input county_id	ammonia nitrogen voc;
DATALINES;
1001	568.76	2329.47	6695.85
1003	0.24	0.00	50.41
1005	10.73	347.76	325.23
1007	0.00	59.06	77.60
1009	4.99	0.00	0.00
RUN;
DATA TRI;
input county_id	voc;
DATALINES;
1001	432.47
1003	0.00
1005	3.22
1007	0.00
1009	1.52
RUN;
data _null_;
  if 0 then set low_2002;
  if _n_=1 then do;
    declare hash h(dataset:'low_2002',ordered:'a');
      h.definekey('county_id');
      h.definedata(all:'Y');
      h.definedone();
  end;
  set tri (rename=(voc=new_voc));
  h.find();
  tmp_voc=voc;
  voc=new_voc;
  h.replace();
  h.output(dataset:cats('low_2002_tri_',_n_));
  voc=tmp_voc;
  h.replace();
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Among the advantages of the above is that you don't have to know in advance how many datasets will be produced.&amp;nbsp; Instead just dynamically generate as many dataset names as needed by constructing the dataset names in the hash OUTPUT method.&amp;nbsp; It will produce as many datasets as there are observations in TRI.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;BTW, this program assumes there is a one-to-one correspondence of COUNTY_ID in TRI to COUNTY_ID in LOW_2002.&lt;/P&gt;</description>
      <pubDate>Mon, 04 Apr 2022 00:11:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-values-and-outputting-data-as-separate-csv-file/m-p/805760#M317403</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-04-04T00:11:35Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing values and outputting data as separate csv file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-values-and-outputting-data-as-separate-csv-file/m-p/805764#M317406</link>
      <description>&lt;P&gt;Thanks! That's really smart and efficient way of doing it. Do you have any ideas how to accomplish the last part? I would like to export each dataset to csv without the county_id variable. So for example, for Low_2002_tri_1, I would do:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data Low_2002_tri_1(drop=county_id);
set Low_2002_tri_1;
run;

proc export data=Low_2002_tri_1
 	outfile="E:\folder\Low_2002_tri_1.csv"
	dbms=csv
	replace;
	putnames=no;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;But, how can I loop this for all datasets?&lt;/P&gt;</description>
      <pubDate>Mon, 04 Apr 2022 00:37:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-values-and-outputting-data-as-separate-csv-file/m-p/805764#M317406</guid>
      <dc:creator>elbarto</dc:creator>
      <dc:date>2022-04-04T00:37:16Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing values and outputting data as separate csv file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-values-and-outputting-data-as-separate-csv-file/m-p/805770#M317407</link>
      <description>&lt;P&gt;Ah nevermind, I figured it out by writing a macro, thanks again!&lt;/P&gt;</description>
      <pubDate>Mon, 04 Apr 2022 02:43:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-values-and-outputting-data-as-separate-csv-file/m-p/805770#M317407</guid>
      <dc:creator>elbarto</dc:creator>
      <dc:date>2022-04-04T02:43:11Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing values and outputting data as separate csv file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-values-and-outputting-data-as-separate-csv-file/m-p/805878#M317467</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/303550"&gt;@elbarto&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks! That's really smart and efficient way of doing it. Do you have any ideas how to accomplish the last part? I would like to export each dataset to csv without the county_id variable. So for example, for Low_2002_tri_1, I would do:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=""&gt;data Low_2002_tri_1(drop=county_id);
set Low_2002_tri_1;
run;

proc export data=Low_2002_tri_1
 	outfile="E:\folder\Low_2002_tri_1.csv"
	dbms=csv
	replace;
	putnames=no;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But, how can I loop this for all datasets?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Pretty much every where you use a data set you can use Data set Options to drop, keep or rename variables.&lt;/P&gt;
&lt;P&gt;So&lt;/P&gt;
&lt;PRE&gt;proc export data=Low_2002_tri_1 (drop=country_id)
 	outfile="E:\folder\Low_2002_tri_1.csv"
	dbms=csv
	replace;
	putnames=no;
run;&lt;/PRE&gt;
&lt;P&gt;No need for that potentially dangerous data step to corrupt your data set.&lt;/P&gt;</description>
      <pubDate>Mon, 04 Apr 2022 15:43:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-values-and-outputting-data-as-separate-csv-file/m-p/805878#M317467</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-04-04T15:43:43Z</dc:date>
    </item>
  </channel>
</rss>

