<?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: Replace if a macro value with a value in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Replace-if-a-macro-value-with-a-value/m-p/601258#M16702</link>
    <description>&lt;P&gt;A good way is to construct your list of values in a SAS dataset then use PROC SQL to write this out to a macro variable:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data MySASDataset;
input @1 MyVar $10.;
cards;
SUBJ-M
SUBJ-J
SUBJ-005-K
SUBJ-007-K
SUBJ-N
;
run;

proc sql noprint;
  select MyVar
  into :MyMacroVar separated by " " 
  from MySASDataset
  ;
quit;


&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sun, 03 Nov 2019 22:12:34 GMT</pubDate>
    <dc:creator>SASKiwi</dc:creator>
    <dc:date>2019-11-03T22:12:34Z</dc:date>
    <item>
      <title>Replace if a macro value with a value</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Replace-if-a-macro-value-with-a-value/m-p/601257#M16701</link>
      <description>&lt;P&gt;How do we use macro objects/variables when we have a list of values we want to replace with?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example, %list contains 001 002 003 004. Let's say we have subject IDs like this:&lt;/P&gt;&lt;P&gt;SUBJ-001-M&lt;/P&gt;&lt;P&gt;SUBJ-002-J&lt;/P&gt;&lt;P&gt;SUBJ-005-K&lt;/P&gt;&lt;P&gt;SUBJ-007-K&lt;/P&gt;&lt;P&gt;SUBJ-004-N&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to say: replace the number between - and - to single - if the number is one of the macro objects, and eventually end up with:&amp;nbsp;&lt;/P&gt;&lt;P&gt;SUBJ-M&lt;/P&gt;&lt;P&gt;SUBJ-J&lt;/P&gt;&lt;P&gt;SUBJ-005-K&lt;/P&gt;&lt;P&gt;SUBJ-007-K&lt;/P&gt;&lt;P&gt;SUBJ-N&lt;/P&gt;</description>
      <pubDate>Sun, 03 Nov 2019 21:52:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Replace-if-a-macro-value-with-a-value/m-p/601257#M16701</guid>
      <dc:creator>gsk</dc:creator>
      <dc:date>2019-11-03T21:52:00Z</dc:date>
    </item>
    <item>
      <title>Re: Replace if a macro value with a value</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Replace-if-a-macro-value-with-a-value/m-p/601258#M16702</link>
      <description>&lt;P&gt;A good way is to construct your list of values in a SAS dataset then use PROC SQL to write this out to a macro variable:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data MySASDataset;
input @1 MyVar $10.;
cards;
SUBJ-M
SUBJ-J
SUBJ-005-K
SUBJ-007-K
SUBJ-N
;
run;

proc sql noprint;
  select MyVar
  into :MyMacroVar separated by " " 
  from MySASDataset
  ;
quit;


&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 03 Nov 2019 22:12:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Replace-if-a-macro-value-with-a-value/m-p/601258#M16702</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2019-11-03T22:12:34Z</dc:date>
    </item>
    <item>
      <title>Re: Replace if a macro value with a value</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Replace-if-a-macro-value-with-a-value/m-p/601279#M16705</link>
      <description>Can you assume each line will have at most one number This is pretty well structured so it could be simplified quite a bit if needed.</description>
      <pubDate>Mon, 04 Nov 2019 01:15:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Replace-if-a-macro-value-with-a-value/m-p/601279#M16705</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-11-04T01:15:29Z</dc:date>
    </item>
    <item>
      <title>Re: Replace if a macro value with a value</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Replace-if-a-macro-value-with-a-value/m-p/601281#M16706</link>
      <description>&lt;P&gt;Why a macro? Seems like you can do this easily in a data step.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 04 Nov 2019 01:57:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Replace-if-a-macro-value-with-a-value/m-p/601281#M16706</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-11-04T01:57:25Z</dc:date>
    </item>
    <item>
      <title>Re: Replace if a macro value with a value</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Replace-if-a-macro-value-with-a-value/m-p/601301#M16707</link>
      <description>How? I don’t think tranwrd or replace would work because they only work with certain expression (not macro containing a list of values)</description>
      <pubDate>Mon, 04 Nov 2019 04:28:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Replace-if-a-macro-value-with-a-value/m-p/601301#M16707</guid>
      <dc:creator>gsk</dc:creator>
      <dc:date>2019-11-04T04:28:05Z</dc:date>
    </item>
    <item>
      <title>Re: Replace if a macro value with a value</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Replace-if-a-macro-value-with-a-value/m-p/601302#M16708</link>
      <description>Thank you for the response! I meant how do we get to MySASDataset if we have a list of values stored in &amp;amp;list?</description>
      <pubDate>Mon, 04 Nov 2019 04:29:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Replace-if-a-macro-value-with-a-value/m-p/601302#M16708</guid>
      <dc:creator>gsk</dc:creator>
      <dc:date>2019-11-04T04:29:24Z</dc:date>
    </item>
    <item>
      <title>Re: Replace if a macro value with a value</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Replace-if-a-macro-value-with-a-value/m-p/601303#M16709</link>
      <description>Yes, at most one number/match.</description>
      <pubDate>Mon, 04 Nov 2019 04:30:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Replace-if-a-macro-value-with-a-value/m-p/601303#M16709</guid>
      <dc:creator>gsk</dc:creator>
      <dc:date>2019-11-04T04:30:47Z</dc:date>
    </item>
    <item>
      <title>Re: Replace if a macro value with a value</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Replace-if-a-macro-value-with-a-value/m-p/601309#M16712</link>
      <description>&lt;P&gt;Hey gsk, is this what you mean? I agree that a data step is most suitable but I wrote a macro as well. It's all very much tailored this this situation, not at all general.&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;%let list = 001 002 003 004;

data have;
input @1 subjects $10.;
cards;
SUBJ-001-M
SUBJ-002-J
SUBJ-005-K
SUBJ-007-K
SUBJ-004-N
;
run;

data want(drop=s1 s2 s3);
	set have;
	s1 = scan(subjects,1,"-");
	s2 = scan(subjects,2,"-");
	s3 = scan(subjects,3,"-");
	if s2 in (&amp;amp;list) then subjects=catx("-",s1,s3);
run;

*or if you like a macro...;

%macro fix_subj(dsin=,dsout=,subjvar=,mylist=);
	data &amp;amp;dsout(drop=s1 s2 s3);
		set &amp;amp;dsin;
		s1 = scan(&amp;amp;subjvar,1,"-");
		s2 = scan(&amp;amp;subjvar,2,"-");
		s3 = scan(&amp;amp;subjvar,3,"-");
		if s2 in (&amp;amp;mylist) then &amp;amp;subjvar=catx("-",s1,s3);
	run;
%mend;

%fix_subj(dsin=have,dsout=want,subjvar=subjects,mylist=&amp;amp;list);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;-unison&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 04 Nov 2019 06:19:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Replace-if-a-macro-value-with-a-value/m-p/601309#M16712</guid>
      <dc:creator>unison</dc:creator>
      <dc:date>2019-11-04T06:19:09Z</dc:date>
    </item>
    <item>
      <title>Re: Replace if a macro value with a value</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Replace-if-a-macro-value-with-a-value/m-p/601323#M16713</link>
      <description>&lt;P&gt;Here a way using a regular expression&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let list = 001 002 003 004;

data have;
  input subjects $10.;
  cards;
SUBJ-001-M
SUBJ-002-J
SUBJ-005-K
SUBJ-007-K
SUBJ-004-N
;

data _null_;
  length listRegEx $100;
  listRegEx=cats('-',tranwrd(compbl("&amp;amp;list"),'','-|-'),'-');
  call symputx('listRegEx',listRegEx);
  stop;
run;

data want;
  set have;
  length subjects2 $10;
  subjects2=prxchange("s/&amp;amp;listRegEx/-/o",1,strip(subjects));
run;

proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 04 Nov 2019 09:32:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Replace-if-a-macro-value-with-a-value/m-p/601323#M16713</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2019-11-04T09:32:27Z</dc:date>
    </item>
    <item>
      <title>Re: Replace if a macro value with a value</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Replace-if-a-macro-value-with-a-value/m-p/601326#M16714</link>
      <description>&lt;P&gt;Thank you so much! This is great. Don’t know why I can only accept one post as a solution &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 04 Nov 2019 09:42:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Replace-if-a-macro-value-with-a-value/m-p/601326#M16714</guid>
      <dc:creator>gsk</dc:creator>
      <dc:date>2019-11-04T09:42:46Z</dc:date>
    </item>
    <item>
      <title>Re: Replace if a macro value with a value</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Replace-if-a-macro-value-with-a-value/m-p/601359#M16716</link>
      <description>&lt;PRE&gt;%let list = 001 002 003 004;

data have;
input @1 subjects $10.;
cards;
SUBJ-001-M
SUBJ-002-J
SUBJ-005-K
SUBJ-007-K
SUBJ-004-N
;
run;

data want;
	set have;
	call  scan(subjects,2,p,l,"-");
	if findw("&amp;amp;list",scan(subjects,2,"-")) then 
subjects=cats(substr(subjects,1,p-1),substr(subjects,p+l+1));
run;&lt;/PRE&gt;</description>
      <pubDate>Mon, 04 Nov 2019 12:57:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Replace-if-a-macro-value-with-a-value/m-p/601359#M16716</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2019-11-04T12:57:05Z</dc:date>
    </item>
  </channel>
</rss>

