<?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: Expressing a large list of character values in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Expressing-a-large-list-of-character-values/m-p/555647#M154641</link>
    <description>&lt;P&gt;So the 2500 character values in question are in a separate data set or?&lt;/P&gt;</description>
    <pubDate>Thu, 02 May 2019 16:05:13 GMT</pubDate>
    <dc:creator>PeterClemmensen</dc:creator>
    <dc:date>2019-05-02T16:05:13Z</dc:date>
    <item>
      <title>Expressing a large list of character values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Expressing-a-large-list-of-character-values/m-p/555645#M154639</link>
      <description>&lt;P&gt;Hello Community,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a very large list (n=2,500) of character values of a variable that I need to define in an IF THEN statement. For example:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;IF varA in ('a', 'b', 'c', 'd' .... &lt;EM&gt;n=2,500&lt;/EM&gt;) THEN newvar=1;&lt;/P&gt;&lt;P&gt;ELSE newvar=0;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there a shortcut that I can use to define all 2,500 character values in my statement WITHOUT having to add quotation marks around every single value?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Thu, 02 May 2019 16:01:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Expressing-a-large-list-of-character-values/m-p/555645#M154639</guid>
      <dc:creator>wj2</dc:creator>
      <dc:date>2019-05-02T16:01:57Z</dc:date>
    </item>
    <item>
      <title>Re: Expressing a large list of character values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Expressing-a-large-list-of-character-values/m-p/555647#M154641</link>
      <description>&lt;P&gt;So the 2500 character values in question are in a separate data set or?&lt;/P&gt;</description>
      <pubDate>Thu, 02 May 2019 16:05:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Expressing-a-large-list-of-character-values/m-p/555647#M154641</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-05-02T16:05:13Z</dc:date>
    </item>
    <item>
      <title>Re: Expressing a large list of character values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Expressing-a-large-list-of-character-values/m-p/555648#M154642</link>
      <description>&lt;P&gt;Please try below approach&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I used sashelp.class dataset with name variable&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select cats("'",name,"'") into: nam separated ',' from sashelp.class;
quit;

%put &amp;amp;nam;

data want;
set sashelp.class;
if name in (&amp;amp;nam) then flag=1;
else flag=.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 02 May 2019 16:08:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Expressing-a-large-list-of-character-values/m-p/555648#M154642</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2019-05-02T16:08:24Z</dc:date>
    </item>
    <item>
      <title>Re: Expressing a large list of character values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Expressing-a-large-list-of-character-values/m-p/555652#M154643</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12151"&gt;@Jagadishkatam&lt;/a&gt;,&amp;nbsp;this looks to be a very good approach! However, how/where would I list the specific values of the name variable (e.g., Alice, John, Jane) that I am interested in defining for the variable flag? Thanks!&lt;/P&gt;</description>
      <pubDate>Thu, 02 May 2019 16:34:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Expressing-a-large-list-of-character-values/m-p/555652#M154643</guid>
      <dc:creator>wj2</dc:creator>
      <dc:date>2019-05-02T16:34:23Z</dc:date>
    </item>
    <item>
      <title>Re: Expressing a large list of character values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Expressing-a-large-list-of-character-values/m-p/555658#M154646</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/115194"&gt;@wj2&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12151"&gt;@Jagadishkatam&lt;/a&gt;,&amp;nbsp;this looks to be a very good approach! However, how/where would I list the specific values of the name variable (e.g., Alice, John, Jane) that I am interested in defining for the variable flag? Thanks!&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I really don't understand what you are asking for.&lt;/P&gt;
&lt;P&gt;Where is the list now? Isn't already in a dataset?&lt;BR /&gt;If not then make one.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data mylist;
  infile cards truncover ;
  input word $100.;
cards;
Alice
John
Jane
Bill Smith
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Of course once you have it in a dataset then no need for quotes at all to create your new variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql ;
create table want as
select *,name in (select word from mylist) as flag
from sashelp.class
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 02 May 2019 16:51:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Expressing-a-large-list-of-character-values/m-p/555658#M154646</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-05-02T16:51:54Z</dc:date>
    </item>
    <item>
      <title>Re: Expressing a large list of character values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Expressing-a-large-list-of-character-values/m-p/555659#M154647</link>
      <description>&lt;P&gt;Please replace the dataset sashelp.class and name variable with your dataset and variable and follow the code logic&lt;/P&gt;</description>
      <pubDate>Thu, 02 May 2019 16:52:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Expressing-a-large-list-of-character-values/m-p/555659#M154647</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2019-05-02T16:52:50Z</dc:date>
    </item>
    <item>
      <title>Re: Expressing a large list of character values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Expressing-a-large-list-of-character-values/m-p/555660#M154648</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/115194"&gt;@wj2&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12151"&gt;@Jagadishkatam&lt;/a&gt;,&amp;nbsp;this looks to be a very good approach! However, how/where would I list the specific values of the name variable (e.g., Alice, John, Jane) that I am interested in defining for the variable flag? Thanks!&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Make a data set with the values. If the values already exist in a list somewhere that should not be too difficult. I suspect that you are not making this list up as you go along.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Once you have a data set there are numerous ways such as &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12151"&gt;@Jagadishkatam&lt;/a&gt;&amp;nbsp;, create a custom format or informat and assign the new value using Put or Input, a Proc sql join&lt;/P&gt;</description>
      <pubDate>Thu, 02 May 2019 16:55:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Expressing-a-large-list-of-character-values/m-p/555660#M154648</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-05-02T16:55:06Z</dc:date>
    </item>
    <item>
      <title>Re: Expressing a large list of character values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Expressing-a-large-list-of-character-values/m-p/555704#M154670</link>
      <description>&lt;P&gt;To clarify, I have one variable (e.g., varA) of a data set that contains all the values that I am interested in. These values are already in character format ($254.).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to create a new variable (e.g., newvar) that contains only select values from varA. The issue is that there are 2,500 select values of varA that I need to include in newvar but to my knowledge, you need to put quotations around each character value of in IF THEN statement. Therefore, I'm wondering if there is a way around this to save time and avoid error. Also, I need to create this variable for each subject of a data set, so my coding framework is as follows.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by subjectID;
if first.subjectID then do;
   newvar=0; 
end;
retain newvar;
if varA in ('a','b','c',.....) /* My question is how do I get around manually adding quotation marks here because there is a list of 2,500 values of varA that I need to add here. I already have the list of the 2,500 values that can be copy and pasted here*/
then newvar=1;
if last.subjectID;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Using&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12151"&gt;@Jagadishkatam&lt;/a&gt;&amp;nbsp;example with sashelp.class:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select cats("'",name,"'") into: nam separated ',' from sashelp.class;
quit;

%put &amp;amp;nam;

data want;
set sashelp.class;
if name in (Alice,John,Jane) then flag=1; /*How do I get around needing to add quotation marks to the values here?*/
else flag=.;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 02 May 2019 18:12:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Expressing-a-large-list-of-character-values/m-p/555704#M154670</guid>
      <dc:creator>wj2</dc:creator>
      <dc:date>2019-05-02T18:12:18Z</dc:date>
    </item>
    <item>
      <title>Re: Expressing a large list of character values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Expressing-a-large-list-of-character-values/m-p/555720#M154675</link>
      <description>&lt;P&gt;replace the &lt;CODE class="  language-sas"&gt; &lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;Alice&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;John&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;Jane&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&amp;nbsp; with &amp;amp;nam macro variable we are creating, it carries the quotation and values.&amp;nbsp;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class="  language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 02 May 2019 18:50:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Expressing-a-large-list-of-character-values/m-p/555720#M154675</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2019-05-02T18:50:08Z</dc:date>
    </item>
    <item>
      <title>Re: Expressing a large list of character values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Expressing-a-large-list-of-character-values/m-p/555748#M154687</link>
      <description>&lt;P&gt;Thank you&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12151"&gt;@Jagadishkatam&lt;/a&gt;.&amp;nbsp;I tried the code; however, SAS is still expecting a quoted string of values where indicated. Any suggestions would be much appreciated.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select cats("'",name,"'") into: nam separated ',' from sashelp.class;
quit;

%put &amp;amp;nam;

data want;
set sashelp.class;
if name in (&amp;amp;nam) then flag=1;
else flag=.;
run;

data want2;
set want;
if name in (Alice,John,Jane) then newvar=1; /*SAS is still expecting a quoted string here. How can I define these character values without using quotations?*/
else newvar=0;
run;

proc freq data=want2;
tables newvar;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 02 May 2019 20:02:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Expressing-a-large-list-of-character-values/m-p/555748#M154687</guid>
      <dc:creator>wj2</dc:creator>
      <dc:date>2019-05-02T20:02:13Z</dc:date>
    </item>
    <item>
      <title>Re: Expressing a large list of character values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Expressing-a-large-list-of-character-values/m-p/555765#M154701</link>
      <description>&lt;P&gt;This is really beginning to sound like an XY problem.&amp;nbsp;&amp;nbsp;&lt;A href="http://xyproblem.info/" target="_blank"&gt;http://xyproblem.info/&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;Or perhaps more of a chicken-egg problem.&amp;nbsp;&amp;nbsp;&lt;A href="https://en.wikipedia.org/wiki/Chicken_or_the_egg" target="_blank"&gt;https://en.wikipedia.org/wiki/Chicken_or_the_egg&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How do you propose to teach SAS which values should result in FLAG being set?&lt;/P&gt;
&lt;P&gt;You either have to list them out or provide it with the list in some other form.&lt;/P&gt;
&lt;P&gt;You could download your list to Excel or a text file and then either eliminate the records you don't want. Or add a new column with the flag value.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But there is not really any need to ever use IN with a list of values.&amp;nbsp; But if you did then for a short lists of words that need to be quoted I use this %QLIST() macro.&amp;nbsp;&lt;A href="https://github.com/sasutils/macros/blob/master/qlist.sas" target="_blank"&gt;https://github.com/sasutils/macros/blob/master/qlist.sas&lt;/A&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set sashelp.class ;
  newvar= name in %qlist(Alice John Jane);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If your list has embedded spaces you can use some other character as the delimiter, like |.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set sashelp.class ;
  newvar= name in %qlist(Alice Jones|John Smith|Jane Doe,delimit=|);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 02 May 2019 20:59:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Expressing-a-large-list-of-character-values/m-p/555765#M154701</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-05-02T20:59:36Z</dc:date>
    </item>
    <item>
      <title>Re: Expressing a large list of character values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Expressing-a-large-list-of-character-values/m-p/555951#M154791</link>
      <description>&lt;P&gt;PROC FORMAT or Hash Table .&lt;/P&gt;</description>
      <pubDate>Fri, 03 May 2019 14:52:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Expressing-a-large-list-of-character-values/m-p/555951#M154791</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2019-05-03T14:52:29Z</dc:date>
    </item>
    <item>
      <title>Re: Expressing a large list of character values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Expressing-a-large-list-of-character-values/m-p/556018#M154821</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;,&amp;nbsp;Yes, thank you for your insight. Essentially I am trying to create a binary variable (yes/no) indicating whether or not a patient has a certain diagnosis. If the patient has ANY one diagnosis out my selected list of 2,500 diagnoses then newvar=1. I already have the list of 2,500 diagnoses of interest compiled (i.e., in a Word document) and they are simply listed together separated by commas (e.g., a,b,c,d,e,f,....). I am wondering how I can just simply copy and paste that list in a code in order to create this variable.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My current coding approach is as shown below; however, this approach requires that I add quotations around each value (since it is a character variable), which would be too time consuming and prone to error. Now that I have provided a better description of the problem, any suggestions would be GREATLY appreciated.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by subjectID;
if first.subjectID then do;
   newvar=0; 
end;
retain newvar;
if varA in ('a','b','c',...) /* This approach requires that I add quotations around each value. Is there an alternate approach where I can simply copy and past my list of 2,500 values (in format a,b,c,d, etc.) into a code to create the variable?*/
then newvar=1;
if last.subjectID;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 03 May 2019 17:22:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Expressing-a-large-list-of-character-values/m-p/556018#M154821</guid>
      <dc:creator>wj2</dc:creator>
      <dc:date>2019-05-03T17:22:28Z</dc:date>
    </item>
    <item>
      <title>Re: Expressing a large list of character values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Expressing-a-large-list-of-character-values/m-p/556030#M154824</link>
      <description>&lt;P&gt;So I answered that already, but let's spell it out in more detail.&amp;nbsp; Take your list from WORD and put in into a text file. Or you could even just paste it into your program as in-line data.&amp;nbsp; Then you can read it into a dataset and use the dataset to match to your list of subjects.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data list ;
  length disease $100;
  infile cards dsd flowover ;
  input disease @@;
  if disease ne ' ';
cards4;
headache,gout,knee pain
cancer,diabetes
;;;;

proc sql noprint ;
create table want as 
select subjectID
     , max( diagnosis in (select disease from list) ) as FLAG
from have
group by subjectid
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You might need to clean up data you have pulled from WORD to make it acceptable as data.&amp;nbsp; Perhaps they have commas in the middle of some of the values?&amp;nbsp; WORD could have converted some of the characters in the strings to "pretty" versions so that they will no longer match the values in your data.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You will probably need to do a little work to make sure that the values in your list actually match the values in your data.&amp;nbsp; Perhaps you want to force everything to uppercase to make matching easier?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;upcase(diagnosis) in (select upcase(disease) from list)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or once you have the data in dataset the other answers with examples of how to pull the values out into macro variables will also work.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
select quote(trim(disease)) into :mylist separated by ' '
from list ;
quit;

data want;
  set have;
  flag = diagnosis in (&amp;amp;mylist);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 03 May 2019 18:19:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Expressing-a-large-list-of-character-values/m-p/556030#M154824</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-05-03T18:19:57Z</dc:date>
    </item>
  </channel>
</rss>

