<?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: output based on variable in dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/output-based-on-variable-in-dataset/m-p/798978#M314109</link>
    <description>&lt;P&gt;The split based on Make is needed because I need to report to different entities (customers) based on Make.&amp;nbsp; Actually I am just using the sashelp.cars as an example.&amp;nbsp; The true split is based on csv files inputted from different states and processed through a sas program&amp;nbsp; The results need to be split based on the state.&lt;/P&gt;</description>
    <pubDate>Sun, 27 Feb 2022 17:19:13 GMT</pubDate>
    <dc:creator>Q1983</dc:creator>
    <dc:date>2022-02-27T17:19:13Z</dc:date>
    <item>
      <title>output based on variable in dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/output-based-on-variable-in-dataset/m-p/798914#M314080</link>
      <description>&lt;PRE&gt;%Let rptout     = /myshare; 

data have;
set sashelp.cars;
run;



%macro eof (Make);
Data _Null_;
   Set have;
   FILE "&amp;amp;rptout/&amp;amp;Make._report1.csv" DLM="," dsd;
      RUN;
	  %mend;
	  %eof(Acura,'Acura');
	  %eof(Audi,'Audi');
	  %eof(BMW,'BMW');&lt;/PRE&gt;
&lt;P&gt;&lt;FONT face="Courier New" color="#ff0000"&gt;1. Is&lt;/FONT&gt;&lt;FONT face="Courier New"&gt; there a way to automate programmatically and segment into separate datasets by make? (ie if make = &lt;/FONT&gt;&lt;FONT face="Courier New" color="#800080"&gt;'Acura'&lt;/FONT&gt;&lt;FONT face="Courier New"&gt; output Acura)&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;2. After executing step 1 is there a way to &lt;/FONT&gt;&lt;FONT face="Courier New"&gt;proc export based on the number of datasets &lt;/FONT&gt;&lt;FONT face="Courier New" color="#008080"&gt;created.&lt;/FONT&gt;&lt;FONT face="Courier New"&gt; Objective is to assign the Make as the name of each dataset at export&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 27 Feb 2022 01:41:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/output-based-on-variable-in-dataset/m-p/798914#M314080</guid>
      <dc:creator>Q1983</dc:creator>
      <dc:date>2022-02-27T01:41:12Z</dc:date>
    </item>
    <item>
      <title>Re: output based on variable in dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/output-based-on-variable-in-dataset/m-p/798917#M314082</link>
      <description>&lt;P&gt;Creating multiple text files is simple. Use the FILEVAR= option on the FILE statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set have;
  length filevar $256 ;
  filevar=cats("&amp;amp;rptout/",make,'.csv');
  file csv dsd filevar=filevar ;
  put (_all_) (+0);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Creating separate datasets is harder than creating separated text files.&amp;nbsp; For that you would need to use code generation.&lt;/P&gt;</description>
      <pubDate>Sun, 27 Feb 2022 02:21:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/output-based-on-variable-in-dataset/m-p/798917#M314082</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-02-27T02:21:36Z</dc:date>
    </item>
    <item>
      <title>Re: output based on variable in dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/output-based-on-variable-in-dataset/m-p/798975#M314107</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/5629"&gt;@Q1983&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You ask for two things:&lt;/P&gt;
&lt;P&gt;1) S&lt;SPAN&gt;egment into separate datasets by make, and &lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;2) Proc export with mMake as the name of each dataset at export.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I think the simplest solution to both problems is to use code generation as mentioned by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt; . The idea is to make a list of Makes and execute a series of Data- or Proc Export steps, each using a subset of data that contains a single make. So the principle is:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;* Get list of makes;
proc sql;
  create table mlist as
    select distinct make
    from sashelp.cars;
quit;

data _null_; 
  set mlist;
  code = 'Program step to do something based on current observarion in input data';
  call execute(code);
run;
  
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;The code for a Data Step or Proc Export to handle a single Make is (example):&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Acura;
  set sashelp.cars where=(make="Acura"));
run;

proc export data=sashelp.cars where=(make="Acura"))
  outfile="c:\temp\Acura.csv"
  dbms=csv; 
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;So this code should be inculded in the Data Step with Call Execute and modified, so the hardcoded "Acura" in the examples is substituted with the current Data Step variable Make. Note that it is necessary to remove hyphens and embedded blanks from Make to get a valid SAS Data Set name.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;The full working code:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;* Get list of makes;
proc sql;
  create table mlist as
    select distinct make
    from sashelp.cars;
quit;

* Split in named data sets;
data _null_; set mlist;
  code = cat('data ',compress(make,'- '),'; set sashelp.cars; where make="',make,'"; run;');
  call execute(code);
run;

* export to named files;
data _null_; 
  set mlist;
  code = cat('proc export data=sashelp.cars(where=(make="',make,'"))outfile="c:\temp\',make,'.csv" dbms=csv; run;');
  call execute(code);
run;
&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;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 27 Feb 2022 15:06:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/output-based-on-variable-in-dataset/m-p/798975#M314107</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2022-02-27T15:06:33Z</dc:date>
    </item>
    <item>
      <title>Re: output based on variable in dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/output-based-on-variable-in-dataset/m-p/798977#M314108</link>
      <description>&lt;P&gt;The question I have for you is: why? Why do you want to do such a thing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Normally, using BY statements or WHERE statements suffice for any analysis in SAS that you might want to do. There is rarely a need to take a dataset and split it into piece by the value of a variable.&lt;/P&gt;</description>
      <pubDate>Sun, 27 Feb 2022 16:53:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/output-based-on-variable-in-dataset/m-p/798977#M314108</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-02-27T16:53:14Z</dc:date>
    </item>
    <item>
      <title>Re: output based on variable in dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/output-based-on-variable-in-dataset/m-p/798978#M314109</link>
      <description>&lt;P&gt;The split based on Make is needed because I need to report to different entities (customers) based on Make.&amp;nbsp; Actually I am just using the sashelp.cars as an example.&amp;nbsp; The true split is based on csv files inputted from different states and processed through a sas program&amp;nbsp; The results need to be split based on the state.&lt;/P&gt;</description>
      <pubDate>Sun, 27 Feb 2022 17:19:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/output-based-on-variable-in-dataset/m-p/798978#M314109</guid>
      <dc:creator>Q1983</dc:creator>
      <dc:date>2022-02-27T17:19:13Z</dc:date>
    </item>
    <item>
      <title>Re: output based on variable in dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/output-based-on-variable-in-dataset/m-p/798979#M314110</link>
      <description>&lt;P&gt;You do not need to split the dataset, you only export in one step to different files, as &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;has already shown.&lt;/P&gt;</description>
      <pubDate>Sun, 27 Feb 2022 17:49:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/output-based-on-variable-in-dataset/m-p/798979#M314110</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-02-27T17:49:11Z</dc:date>
    </item>
    <item>
      <title>Re: output based on variable in dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/output-based-on-variable-in-dataset/m-p/798981#M314111</link>
      <description>&lt;P&gt;Saying the results need to be in a certain form does not explain &lt;STRONG&gt;WHY&lt;/STRONG&gt; they need to be in a certain form.&lt;/P&gt;</description>
      <pubDate>Sun, 27 Feb 2022 17:57:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/output-based-on-variable-in-dataset/m-p/798981#M314111</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-02-27T17:57:02Z</dc:date>
    </item>
    <item>
      <title>Re: output based on variable in dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/output-based-on-variable-in-dataset/m-p/798984#M314112</link>
      <description>&lt;P&gt;Try this, it skips the step of creating subsets and writes the datasets directly to a CSV file for each name in the CLASS data set with a header row.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This approach will not work if you have a large number of variables - the macro variable lists have a limit of 64k characters.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;*name of the data set with the original data;
%let lib_name = sashelp;
%let dsn_name = class;
*Variable to split on;
%let var_split = NAME;
*path to folder to save text files;
%let path_folder= /home/fkhurshed/;




*if you are exporting each line this is not required 
but should not  cause any issues unless your data set is large. In that case rename your data set to _temp and skip this step;
PROC SORT DATA=&amp;amp;lib_name..&amp;amp;dsn_name OUT=_temp;
BY &amp;amp;var_split;
RUN;



*make variable lists;
*for header row;
proc sql noprint;
select name into :var_list_csv separated by ", " from sashelp.vcolumn where libname = upper("&amp;amp;lib_name") and memname = upper("&amp;amp;dsn_name");
select name into :var_list separated by " " from sashelp.vcolumn where libname = upper("&amp;amp;lib_name") and memname = upper("&amp;amp;dsn_name");
quit;



DATA _NULL_;

SET _temp; *Dataset to be exported;
BY &amp;amp;var_split.; *Variable that file is to be split on;

*Create path to file that is to be exported;
if first.&amp;amp;var_split. then out_file=cats("&amp;amp;path_folder.", &amp;amp;var_split., ".csv");

file temp filevar=out_file dlm=',' dsd;

*If first value of make then output column names;
if first.&amp;amp;var_split. then 
put "&amp;amp;var_list_csv.";

*Output variables;
put &amp;amp;var_list.;

run;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/5629"&gt;@Q1983&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;PRE&gt;%Let rptout     = /myshare; 

data have;
set sashelp.cars;
run;



%macro eof (Make);
Data _Null_;
   Set have;
   FILE "&amp;amp;rptout/&amp;amp;Make._report1.csv" DLM="," dsd;
      RUN;
	  %mend;
	  %eof(Acura,'Acura');
	  %eof(Audi,'Audi');
	  %eof(BMW,'BMW');&lt;/PRE&gt;
&lt;P&gt;&lt;FONT face="Courier New" color="#ff0000"&gt;1. Is&lt;/FONT&gt;&lt;FONT face="Courier New"&gt; there a way to automate programmatically and segment into separate datasets by make? (ie if make = &lt;/FONT&gt;&lt;FONT face="Courier New" color="#800080"&gt;'Acura'&lt;/FONT&gt;&lt;FONT face="Courier New"&gt; output Acura)&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;2. After executing step 1 is there a way to &lt;/FONT&gt;&lt;FONT face="Courier New"&gt;proc export based on the number of datasets &lt;/FONT&gt;&lt;FONT face="Courier New" color="#008080"&gt;created.&lt;/FONT&gt;&lt;FONT face="Courier New"&gt; Objective is to assign the Make as the name of each dataset at export&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 27 Feb 2022 18:07:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/output-based-on-variable-in-dataset/m-p/798984#M314112</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-02-27T18:07:39Z</dc:date>
    </item>
    <item>
      <title>Re: output based on variable in dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/output-based-on-variable-in-dataset/m-p/798986#M314114</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Creating separate datasets is harder than creating separated text files.&amp;nbsp; For that you would need to use code generation.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;But i&lt;/P&gt;
&lt;P&gt;f you are willing to use a set hashes (and a hash of hashes), then you don't have to use code generation to create one dataset per make:&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=" language-sas"&gt;data have;
  set sashelp.cars;
run;

data _null_; 
  set have end=end_of_have;

  if _n_=1 then do;
    declare hash h;

    declare hash hoh (ordered:'a');
      hoh.definekey('make');
      hoh.definedata('make','h');
      hoh.definedone(); 
    declare hiter hohi ('hoh');
  end;

  if hoh.find()^=0 then do;
    h=_new_ hash(dataset:'have (obs=0)');   
    * or (see note) h=_new_ hash(dataset:'have (obs=0)',multidata:'Y');
      h.definekey(all:'Y');
      *or (see note) h.definekey('make');
      h.definedata(all:'Y');
      h.definedone();
    hoh_add=hoh.add();
  end;
  h.add();

  if end_of_have then do rc=hohi.first() by 0 until (hohi.next()^=0);
    h.output(dataset:translate(trim(make),'___',' -&amp;amp;'));
  end;
run;&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;The program above creates one hash object per make.&amp;nbsp; Note that the hash object is named &lt;EM&gt;&lt;STRONG&gt;h&lt;/STRONG&gt;&lt;/EM&gt;&amp;nbsp;for every instance - i.e. for each make.&amp;nbsp; It is use of the hash-of-hashes object &lt;EM&gt;&lt;STRONG&gt;hoh&lt;/STRONG&gt;&lt;/EM&gt; that manages those individual hash objects such that each incoming observation becomes a data item in the associated hash &lt;EM&gt;&lt;STRONG&gt;h&lt;/STRONG&gt;&lt;/EM&gt;.&lt;BR /&gt;&lt;BR /&gt;Now the hash objects &lt;EM&gt;&lt;STRONG&gt;h&lt;/STRONG&gt;&lt;/EM&gt; are &lt;EM&gt;&lt;STRONG&gt;keyed on all variables&lt;/STRONG&gt;&lt;/EM&gt; found in HAVE.&amp;nbsp; Assuming no observations are completely identical, this will avoid the problem of dealing with "collisions" in the hash objects, whose default is to keep only one dataitem (think row) per set of key values.&amp;nbsp; That would be a problem if the &lt;EM&gt;&lt;STRONG&gt;h&lt;/STRONG&gt;&lt;/EM&gt; objects were keyed only on make.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The primary downside of this is that you need enough memory to hold all the data from HAVE (and all the memory to record the complete set of keys).&amp;nbsp; That's not a problem here, but it could be a problem for a large data set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There would be many ways to mitigate this problem.&amp;nbsp; One way to partially reduce memory is to substitute the commented lines above for their respective preceding statements.&amp;nbsp; This allows the variable MAKE to be the sole key, reducing memory demand.&amp;nbsp; But it also obligates the programmer to accommodate duplicate key values - hence the &lt;EM&gt;&lt;STRONG&gt;multidata:'Y'&lt;/STRONG&gt;&lt;/EM&gt; option.&lt;BR /&gt;&lt;BR /&gt;And if memory is still a problem you could list the anticipated make's as explicit dataset names in the DATA statement, then use the hash objects for unanticipated makes.&amp;nbsp; Let's say you know you will have datasets &lt;EM&gt;&lt;STRONG&gt;Chevrolet&lt;/STRONG&gt;&lt;/EM&gt;, &lt;EM&gt;&lt;STRONG&gt;Ford&lt;/STRONG&gt;&lt;/EM&gt;, and &lt;EM&gt;&lt;STRONG&gt;Toyota&lt;/STRONG&gt;&lt;/EM&gt;:&amp;nbsp; All others are unanticipated. Then this would use a lot less memory:&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=" language-sas"&gt;data Chevrolet (where=(make='Chevrolet'))
     Ford      (where=(make='Ford'))
     Toyota    (where=(make='Toyota'));
  set have end=end_of_have;

  if _n_=1 then do;
    declare hash h;
    declare hash hoh (ordered:'a');
      hoh.definekey('make');
      hoh.definedata('make','h');
      hoh.definedone(); 
    declare hiter hohi ('hoh');
  end;
  
  if findw('Chevrolet Ford Toyota',trim(make),' ','e') then do;
     output;
     return;
  end;

  if hoh.find()^=0 then do;
    h=_new_ hash(dataset:'have (obs=0)',multidata:'Y');
      h.definekey('make');
      h.definedata(all:'Y');
      h.definedone();
    hoh_add=hoh.add();
  end;
  h.add();

  if end_of_have then do rc=hohi.first() by 0 until (hohi.next()^=0);
    h.output(dataset:translate(trim(make),'___',' -&amp;amp;'));
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 27 Feb 2022 18:12:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/output-based-on-variable-in-dataset/m-p/798986#M314114</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-02-27T18:12:13Z</dc:date>
    </item>
    <item>
      <title>Re: output based on variable in dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/output-based-on-variable-in-dataset/m-p/798987#M314115</link>
      <description>&lt;P&gt;Why are you runing the same SQL query twice?&lt;BR /&gt;Instead of two queries like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select name into :var_list_csv separated by ", " from sashelp.vcolumn where libname = upper("&amp;amp;lib_name") and memname = upper("&amp;amp;dsn_name");
select name into :var_list separated by " " from sashelp.vcolumn where libname = upper("&amp;amp;lib_name") and memname = upper("&amp;amp;dsn_name");
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Just use one:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select name 
     , name
  into :var_list_csv separated by ", " 
     , :var_list separated by " " 
  from sashelp.vcolumn 
  where libname = upper("&amp;amp;lib_name") and memname = upper("&amp;amp;dsn_name")
;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 27 Feb 2022 18:14:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/output-based-on-variable-in-dataset/m-p/798987#M314115</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-02-27T18:14:39Z</dc:date>
    </item>
    <item>
      <title>Re: output based on variable in dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/output-based-on-variable-in-dataset/m-p/798991#M314118</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
  select distinct make into :carlist separated by '|'
  from sashelp.cars
  where make is not null;
;
quit;

%put &amp;amp;carlist .;
%macro each;
	%local i next_car;
	%let i=1;
	%do %while (%scan(&amp;amp;carlist, &amp;amp;i) ne );
		%let next_car=%scan(&amp;amp;carlist,&amp;amp;i, |);
		
		data %sysfunc(compress(&amp;amp;next_car)); /* Removes space from Land Rover */
		 set sashelp.cars;
		    where make eq "&amp;amp;next_car";
		run;
				
		proc export  
		     outfile="~/csv/%sysfunc(compress(&amp;amp;next_car)).csv"
		     dbms=csv 
		     replace;
		run;	
		
		  %let i = %eval(&amp;amp;i + 1);
	%end;
%mend;

%each;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="ghosh_0-1645986730862.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/68990iB642937809AA8B93/image-size/medium?v=v2&amp;amp;px=400" role="button" title="ghosh_0-1645986730862.png" alt="ghosh_0-1645986730862.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 27 Feb 2022 18:34:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/output-based-on-variable-in-dataset/m-p/798991#M314118</guid>
      <dc:creator>ghosh</dc:creator>
      <dc:date>2022-02-27T18:34:40Z</dc:date>
    </item>
    <item>
      <title>Re: output based on variable in dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/output-based-on-variable-in-dataset/m-p/798994#M314120</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/5629"&gt;@Q1983&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the problem is just about creating a bunch of csv files, the simple solution is to do it in one step as suggested. But it pays to seperate the looping logic and the task code, if the task code is something more complicated. which I think would be the case in "real life".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;We have lots of running jobs that exports data to individual csv files or spreadsheets for each department, each file with department name and a date suffix. But the task doesn't stop there. The files must be stored in a documentation folder, and mails with a text body specifying actions to take based on actual content + the file must be sent to individual receivers.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The code could easily become very complicated and difficult to maintain, if everything is done in one step with lots of if-first and if-last constructs to handle department boundaries. It is much simpler in my opinion to write a macro that handles one department whith the department name and meil receiver as arguments, and use some control logic to call the macro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have rewritten my previous example to use a macro + control logic. It is not as effecient as one step, because input data must be processed for each make, but I think it is a small price to pay, because it is much simpler to expand the macro to do whatever is wanted, if it works with one object only.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;* Task code for a given make;
%let outfolder = c:\temp;
%macro writefile(make);
  proc export data=sashelp.cars(where=(make="&amp;amp;make"))
    outfile="&amp;amp;outfolder\&amp;amp;make..csv" dbms=csv replace;
  run;
%mend;

* Get list of makes;
proc sql;
  create table mlist as
    select distinct make
    from sashelp.cars;
quit;

* export to named files;
data _null_; 
  set mlist;
  call execute('%writefile(' || make || ');');
run;


&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 27 Feb 2022 19:09:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/output-based-on-variable-in-dataset/m-p/798994#M314120</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2022-02-27T19:09:18Z</dc:date>
    </item>
    <item>
      <title>Re: output based on variable in dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/output-based-on-variable-in-dataset/m-p/799002#M314124</link>
      <description>&lt;P&gt;If you just need individual csv files and not generate individual datasets, the following will do it.&lt;/P&gt;
&lt;P&gt;Note, names with embedded spaces will have the blanks removed&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
  select distinct make into :carlist separated by '|'
  from sashelp.cars
  where make is not null;
;
quit;

%macro each;
	%local i next_car;
	%let i=1;
	%do %while (%scan(&amp;amp;carlist, &amp;amp;i) ne );
		%let next_car=%scan(&amp;amp;carlist,&amp;amp;i, |);		

		proc export  
		   data=sashelp.cars(where=(make="&amp;amp;next_car"))	       
		     outfile="~/csv/%sysfunc(compress(&amp;amp;next_car)).csv"
		     dbms=csv 
		     replace;		  
		run;			
		  %let i = %eval(&amp;amp;i + 1);
	%end;
%mend;

%each;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 27 Feb 2022 20:34:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/output-based-on-variable-in-dataset/m-p/799002#M314124</guid>
      <dc:creator>ghosh</dc:creator>
      <dc:date>2022-02-27T20:34:15Z</dc:date>
    </item>
  </channel>
</rss>

