<?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: Creating Macro Variables using PROC SQL in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Creating-Macro-Variables-using-PROC-SQL/m-p/16310#M2959</link>
    <description>Based on this forum thread example, the code works where the PROC SQL populates a zero on a no-match condition - and you have to run it through twice to catch the front-end and back-end no-match:&lt;BR /&gt;
&lt;BR /&gt;
%let boys = 0,0,Fred,Joe,0,Ted,0;&lt;BR /&gt;
%let boys = %sysfunc(tranwrd(%nrbquote(&amp;amp;boys),%str(0,),%str(\)));&lt;BR /&gt;
%let boys = %sysfunc(tranwrd(%nrbquote(&amp;amp;boys),%str(,0),%str(\)));&lt;BR /&gt;
%let boys = %sysfunc(compress(%nrbquote(&amp;amp;boys),\));&lt;BR /&gt;
%put &amp;amp;boys;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
    <pubDate>Fri, 13 Nov 2009 21:20:37 GMT</pubDate>
    <dc:creator>sbb</dc:creator>
    <dc:date>2009-11-13T21:20:37Z</dc:date>
    <item>
      <title>Creating Macro Variables using PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Creating-Macro-Variables-using-PROC-SQL/m-p/16301#M2950</link>
      <description>DATA DEMOG;&lt;BR /&gt;
INPUT NAME $ GENDER $;&lt;BR /&gt;
DATALINES;&lt;BR /&gt;
TOM  M&lt;BR /&gt;
JILL    F&lt;BR /&gt;
PETE M&lt;BR /&gt;
KAT   F&lt;BR /&gt;
SAM  M&lt;BR /&gt;
KELY F&lt;BR /&gt;
;&lt;BR /&gt;
RUN;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
PROC SQL NOPRINT;&lt;BR /&gt;
SELECT CASE WHEN GENDER = 'F' THEN NAME END,&lt;BR /&gt;
             CASE WHEN GENDER = 'M' THEN NAME END &lt;BR /&gt;
INTO :GIRLS SEPARATED BY ','  , &lt;BR /&gt;
        :BOYS  SEPARATED BY ','&lt;BR /&gt;
FROM DEMOG ;&lt;BR /&gt;
QUIT;&lt;BR /&gt;
%PUT &amp;amp;GIRLS. ;&lt;BR /&gt;
%PUT &amp;amp;BOYS. ;&lt;BR /&gt;
&lt;BR /&gt;
Hello,&lt;BR /&gt;
I'm creating 2 macro variables GIRLS, BOYS as stated in the above sql. I'm getting the desired ouput. But i'm getting some unwanted comma's in the output a sshown below.&lt;BR /&gt;
       %PUT &amp;amp;GIRLS. ;&lt;BR /&gt;
&lt;B&gt;,JILL,,KAT,,KELY&lt;/B&gt;&lt;BR /&gt;
       %PUT &amp;amp;BOYS. ;&lt;BR /&gt;
&lt;B&gt;TOM,,PETE,,SAM,&lt;/B&gt;.&lt;BR /&gt;
&lt;BR /&gt;
I dont want to use 2 select statements when creating the macro variables . I can get the desired output if I use the below sql statements.But I want to use either a case or if statement only.&lt;B&gt;can anybody pls help me with this.I really appreciate your help..Thanks&lt;/B&gt;&lt;BR /&gt;
PROC SQL NOPRINT;&lt;BR /&gt;
SELECT NAME INTO: BOYS  SEPARATED BY ',' FROM DEMOG WHERE GENDER = 'M';&lt;BR /&gt;
SELECT NAME INTO: GIRLS SEPARATED BY ',' FROM DEMOG WHERE GENDER = 'F';&lt;BR /&gt;
QUIT;</description>
      <pubDate>Thu, 12 Nov 2009 20:45:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Creating-Macro-Variables-using-PROC-SQL/m-p/16301#M2950</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-11-12T20:45:11Z</dc:date>
    </item>
    <item>
      <title>Re: Creating Macro Variables using PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Creating-Macro-Variables-using-PROC-SQL/m-p/16302#M2951</link>
      <description>What is happening is that in each Case group the values that do not match are being set to Missing and placed in the macro variable.  &lt;BR /&gt;
If you code an ELSE condition for each case setting a default value you will see that value between your ','s.&lt;BR /&gt;
The reason you have them all doubled is that you alternate gender in you data set.</description>
      <pubDate>Thu, 12 Nov 2009 21:01:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Creating-Macro-Variables-using-PROC-SQL/m-p/16302#M2951</guid>
      <dc:creator>Flip</dc:creator>
      <dc:date>2009-11-12T21:01:25Z</dc:date>
    </item>
    <item>
      <title>Re: Creating Macro Variables using PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Creating-Macro-Variables-using-PROC-SQL/m-p/16303#M2952</link>
      <description>Thanks for your reply.&lt;BR /&gt;
If I use an ELSE condition like&lt;BR /&gt;
CASE WHEN GENDER = 'F' THEN NAME ELSE 0 END &lt;BR /&gt;
then values  0 will also be created in the macro variable &amp;amp;GIRLS. shown below.&lt;BR /&gt;
0,JILL,0,KAT,0,KELY&lt;BR /&gt;
 which I don't want in my results.&lt;BR /&gt;
I just want my results like this - JILL,KAT,KELY&lt;BR /&gt;
&lt;BR /&gt;
Thanks</description>
      <pubDate>Thu, 12 Nov 2009 22:56:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Creating-Macro-Variables-using-PROC-SQL/m-p/16303#M2952</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-11-12T22:56:09Z</dc:date>
    </item>
    <item>
      <title>Re: Creating Macro Variables using PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Creating-Macro-Variables-using-PROC-SQL/m-p/16304#M2953</link>
      <description>understand what is happening:&lt;BR /&gt;
"separated by ',' " places comma between selected rows.&lt;BR /&gt;
When you want a females-only list, select where the input is female.&lt;BR /&gt;
 "Case", is not "where".&lt;BR /&gt;
To create two separate lists, you  need two separate "select" statements, like[pre]    select name into :males separated by "," from demog where gender = "M";  &lt;BR /&gt;
   select name into :females separated by "," from demog where gender = "F";&lt;BR /&gt;
[/pre]hope that does it.&lt;BR /&gt;
Alternatively, removing the comma separation from your original hides the empty names of the "wrong" gender.&lt;BR /&gt;
 &lt;BR /&gt;
 PeterC</description>
      <pubDate>Fri, 13 Nov 2009 08:30:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Creating-Macro-Variables-using-PROC-SQL/m-p/16304#M2953</guid>
      <dc:creator>Peter_C</dc:creator>
      <dc:date>2009-11-13T08:30:16Z</dc:date>
    </item>
    <item>
      <title>Re: Creating Macro Variables using PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Creating-Macro-Variables-using-PROC-SQL/m-p/16305#M2954</link>
      <description>One remedy to the current behavior: the resulting macro variables could be post-processed with a couple of %LET statements using %SYSFUNC with the TRANWRD and COMPRESS functions.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
      <pubDate>Fri, 13 Nov 2009 15:24:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Creating-Macro-Variables-using-PROC-SQL/m-p/16305#M2954</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2009-11-13T15:24:11Z</dc:date>
    </item>
    <item>
      <title>Re: Creating Macro Variables using PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Creating-Macro-Variables-using-PROC-SQL/m-p/16306#M2955</link>
      <description>something like&lt;BR /&gt;
%let males =%sysfunc( tranwrd(%superq(males),%str(,,), %str(,) ));&lt;BR /&gt;
with superQ to protect the commas in the value of &amp;amp;males, from being treated as syntax, rather than data&lt;BR /&gt;
Another issue to manage: occasions where more than two commas occur.&lt;BR /&gt;
Here is a small demo&lt;BR /&gt;
%let males = a,b,,c,,,d,,,,e,,,,,,f,,,,,,,g,,,,,,,,h;&lt;BR /&gt;
%let males =%sysfunc( tranwrd(%superq(males),%str(,,), %str(,) ));%put &amp;amp;males ;&lt;BR /&gt;
%let males =%sysfunc( tranwrd(%superq(males),%str(,,), %str(,) ));%put &amp;amp;males ;&lt;BR /&gt;
%let males =%sysfunc( tranwrd(%superq(males),%str(,,), %str(,) ));%put &amp;amp;males ;&lt;BR /&gt;
[pre]1    %let males = a,b,,c,,,d,,,,e,,,,,,f,,,,,,,g,,,,,,,,h;&lt;BR /&gt;
2    %let males =%sysfunc( tranwrd(%superq(males),%str(,,), %str(,) ));%put &amp;amp;males ;&lt;BR /&gt;
a,b,c,,d,,e,,,f,,,,g,,,,h&lt;BR /&gt;
3    %let males =%sysfunc( tranwrd(%superq(males),%str(,,), %str(,) ));%put &amp;amp;males ;&lt;BR /&gt;
a,b,c,d,e,,f,,g,,h&lt;BR /&gt;
4    %let males =%sysfunc( tranwrd(%superq(males),%str(,,), %str(,) ));%put &amp;amp;males ;&lt;BR /&gt;
a,b,c,d,e,f,g,h[/pre]&lt;BR /&gt;
&lt;BR /&gt;
looks like it needs three compbl() invocations to deal with all.</description>
      <pubDate>Fri, 13 Nov 2009 15:45:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Creating-Macro-Variables-using-PROC-SQL/m-p/16306#M2955</guid>
      <dc:creator>Peter_C</dc:creator>
      <dc:date>2009-11-13T15:45:37Z</dc:date>
    </item>
    <item>
      <title>Re: Creating Macro Variables using PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Creating-Macro-Variables-using-PROC-SQL/m-p/16307#M2956</link>
      <description>With the TRANWRD function, use a replacement character that does not appear in the original string and then use COMPRESS to squeeze it out.  I do it all the time in this manner.  Unfortunately, TRANWRD does not understand what to do if you want a null replacement value -- a single blank is a minimum replacement, unfortunately -- so use COMPRESS does the clean-up.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
      <pubDate>Fri, 13 Nov 2009 16:10:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Creating-Macro-Variables-using-PROC-SQL/m-p/16307#M2956</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2009-11-13T16:10:06Z</dc:date>
    </item>
    <item>
      <title>Re: Creating Macro Variables using PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Creating-Macro-Variables-using-PROC-SQL/m-p/16308#M2957</link>
      <description>Scott&lt;BR /&gt;
would you like to demonstrate?&lt;BR /&gt;
I can't achieve the required effect with less than the 3 tramwrd() calls.&lt;BR /&gt;
PeterC</description>
      <pubDate>Fri, 13 Nov 2009 20:07:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Creating-Macro-Variables-using-PROC-SQL/m-p/16308#M2957</guid>
      <dc:creator>Peter_C</dc:creator>
      <dc:date>2009-11-13T20:07:52Z</dc:date>
    </item>
    <item>
      <title>Re: Creating Macro Variables using PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Creating-Macro-Variables-using-PROC-SQL/m-p/16309#M2958</link>
      <description>Not to change the subject completely but I though RegEX would be usefull.  However, I could not get this to work via SYSFUNC.  I always got an error regarding the , in the change expression.  Tried all sorts of quoting but never found the solution.&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
731  data _null_;&lt;BR /&gt;
732     males = 'a,b,,c,,,d,,,,e,,,,,,f,,,,,,,g,,,,,,,,h';&lt;BR /&gt;
733     males =prxchange('s/,+/,/',-1,males);&lt;BR /&gt;
734     put males;&lt;BR /&gt;
735     run;&lt;BR /&gt;
&lt;BR /&gt;
a,b,c,d,e,f,g,h&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
I count not get past the comma.&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
739  %let males = 'a,b,,c,,,d,,,,e,,,,,,f,,,,,,,g,,,,,,,,h';&lt;BR /&gt;
740  %let males =%qsysfunc(prxchange(%qsysfunc(rxparse(%str(s/,+/,/))),-1,%superQ(males)));&lt;BR /&gt;
ERROR: The following pattern expression passed to the function RXPARSE contains a syntax error.&lt;BR /&gt;
s/,+/,/&lt;BR /&gt;
  ^&lt;BR /&gt;
ERROR: Misplaced list separator or missing change expression.&lt;BR /&gt;
WARNING: Argument 1 to function RXPARSE referenced by the %SYSFUNC or %QSYSFUNC macro function is out of range.&lt;BR /&gt;
NOTE: Mathematical operations could not be performed during %SYSFUNC function execution. The result of the operations have&lt;BR /&gt;
      been set to a missing value.&lt;BR /&gt;
NOTE: Argument 1 to the function PRXCHANGE is missing.&lt;BR /&gt;
WARNING: Argument 1 to function PRXCHANGE referenced by the %SYSFUNC or %QSYSFUNC macro function is out of range.&lt;BR /&gt;
741  %put NOTE: MALES=***&amp;amp;Males***;&lt;BR /&gt;
NOTE: MALES=******&lt;BR /&gt;
[/pre]</description>
      <pubDate>Fri, 13 Nov 2009 21:13:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Creating-Macro-Variables-using-PROC-SQL/m-p/16309#M2958</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2009-11-13T21:13:03Z</dc:date>
    </item>
    <item>
      <title>Re: Creating Macro Variables using PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Creating-Macro-Variables-using-PROC-SQL/m-p/16310#M2959</link>
      <description>Based on this forum thread example, the code works where the PROC SQL populates a zero on a no-match condition - and you have to run it through twice to catch the front-end and back-end no-match:&lt;BR /&gt;
&lt;BR /&gt;
%let boys = 0,0,Fred,Joe,0,Ted,0;&lt;BR /&gt;
%let boys = %sysfunc(tranwrd(%nrbquote(&amp;amp;boys),%str(0,),%str(\)));&lt;BR /&gt;
%let boys = %sysfunc(tranwrd(%nrbquote(&amp;amp;boys),%str(,0),%str(\)));&lt;BR /&gt;
%let boys = %sysfunc(compress(%nrbquote(&amp;amp;boys),\));&lt;BR /&gt;
%put &amp;amp;boys;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
      <pubDate>Fri, 13 Nov 2009 21:20:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Creating-Macro-Variables-using-PROC-SQL/m-p/16310#M2959</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2009-11-13T21:20:37Z</dc:date>
    </item>
  </channel>
</rss>

