<?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: How to get a distinct list of words in a variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-a-distinct-list-of-words-in-a-variable/m-p/438700#M109401</link>
    <description>&lt;P&gt;Sorry the code seems to be unreadable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;* split multiple anchors in one record per value;
data anchor; set viswin (where=(anchor^='n/a')); 
	do i = 1 to countw(anchor);
		dset=scan(anchor,i); 
		output;
	end;
run;

proc sql noprint; * noprint to avoid output-display from second select;
	* get distinct list;
	select distinct dset into :anchor_list separated by ' '
		from anchor;

	* count;
	select count(distinct(dset)) into: anchor_count
		from anchor;
quit;
%let anchor_count = &amp;amp;anchor_count; * to get rid of blanks;
%put &amp;amp;=anchor_list;
%put &amp;amp;=anchor_count;&lt;/PRE&gt;</description>
    <pubDate>Tue, 20 Feb 2018 16:25:58 GMT</pubDate>
    <dc:creator>ErikLund_Jensen</dc:creator>
    <dc:date>2018-02-20T16:25:58Z</dc:date>
    <item>
      <title>How to get a distinct list of words in a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-a-distinct-list-of-words-in-a-variable/m-p/438684#M109391</link>
      <description>&lt;P&gt;I have a dataset that is read in from a control file that lists visit IDs, names, and windows as well as anchor datasets if applicable.&amp;nbsp; I then merge this with eCRF data and external lab data to compare visit dates to make sure they are within windows.&amp;nbsp; My concern is that there may be more than one anchor dataset, and I want to be able to get a distinct list of anchor datasets where applicable, but I'm not sure how to go about this.&amp;nbsp; Below is my current thought process, but this is a specific example.&amp;nbsp; I need to generalize my code inside a macro to fit any number of anchor datasets.&amp;nbsp; I think my biggest concern is how to get the maximum number of "words" in the ANCHOR variable so that I can loop through it.&amp;nbsp; There may even be a more efficient way to do it than what I have below.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In short, below&amp;nbsp;is the code to get a sample dataset.&amp;nbsp; I want to take the ANCHOR variable and get a list of distinct values, which in this case would be RD_DOSEL, RD_DOSEL2&lt;SPAN&gt;, RD_DOSEL3, RD_DOSEL4, RD_DOSEL5.&amp;nbsp; I can then use this to loop inside a macro to read each dataset.&lt;/SPAN&gt;&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 viswin;
	input VISITID 1-5 VISITMNEMONIC $ 9-24 MIN_WINDOW 25-27 MAX_WINDOW 29 ANCHOR $ 33-63;
	cards;
30101	Day 1 (Part 1)	0	0	n/a
30115	Day 15 (Pat 1)	-2	2	n/a
30199	SFUP (Part 1)	-7	7	RD_DOSEL, RD_DOSEL2, RD_DOSEL3
30201	Day 1 (Part 2)	0	0	n/a
30215	Day 15 (Part 2)	-2	2	n/a
30299	SFUP (Part 2)	-7	7	RD_DOSEL, RD_DOSEL4
30301	Day 1 (Part 3)	0	0	n/a
30315	Day 15 (Part 3)	-3	3	n/a
30399	SFUP (Part 3)	-7	7	RD_DOSEL, RD_DOSEL5
;
run;&lt;BR /&gt;&lt;BR /&gt;data anchor;&lt;BR /&gt; set viswin (where=(anchor^='n/a'));&lt;BR /&gt; ndset=countw(anchor);&lt;BR /&gt; dset1=scan(anchor,1);&lt;BR /&gt; dset2=scan(anchor,2);&lt;BR /&gt; dset3=scan(anchor,3);&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;data anchor2;&lt;BR /&gt; set anchor (keep=dset1 rename=(dset1=dset))&lt;BR /&gt; anchor (keep=dset2 rename=(dset2=dset))&lt;BR /&gt; anchor (keep=dset3 rename=(dset3=dset));&lt;BR /&gt; if dset='' then delete;&lt;BR /&gt;run;&lt;BR /&gt;proc sort data=anchor2 nodupkey;&lt;BR /&gt; by dset;&lt;BR /&gt;run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 20 Feb 2018 15:54:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-a-distinct-list-of-words-in-a-variable/m-p/438684#M109391</guid>
      <dc:creator>djbateman</dc:creator>
      <dc:date>2018-02-20T15:54:59Z</dc:date>
    </item>
    <item>
      <title>Re: How to get a distinct list of words in a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-a-distinct-list-of-words-in-a-variable/m-p/438689#M109395</link>
      <description>&lt;P&gt;Okay.&amp;nbsp; I have come up with a piece of generalized code.&amp;nbsp; But can someone review this and let me know if there is a more efficient way to do this?&amp;nbsp; The code just seems a little lengthy for what I'm trying to do.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro anchor;
	data anchor;
		set viswin (where=(anchor^='n/a'));
		ndset=countw(anchor);
	run;
	proc sql noprint;
		select max(ndset) into :nanchor from anchor;
		%let nanchor=%sysfunc(compress(&amp;amp;nanchor.));
	quit;
	data anchor;
		set anchor;
		%do i=1 %to &amp;amp;nanchor.;
			dset&amp;amp;i.=scan(anchor,&amp;amp;i.);
		%end;
	run;

	data anchor2;
		set %do i=1 %to &amp;amp;nanchor.;
				anchor (keep=dset&amp;amp;i. rename=(dset&amp;amp;i.=dset))
			%end; ;
		if dset='' then delete;
	run;
	proc sort data=anchor2 nodupkey;
		by dset;
	run;

	proc sql noprint;
		select count(*) into :ndset from anchor2;
		%let ndset=%sysfunc(compress(&amp;amp;ndset.));
		select dset into :dset1-:dset&amp;amp;ndset. from anchor2;
	quit;
%mend anchor;

%anchor;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 20 Feb 2018 16:07:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-a-distinct-list-of-words-in-a-variable/m-p/438689#M109395</guid>
      <dc:creator>djbateman</dc:creator>
      <dc:date>2018-02-20T16:07:41Z</dc:date>
    </item>
    <item>
      <title>Re: How to get a distinct list of words in a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-a-distinct-list-of-words-in-a-variable/m-p/438699#M109400</link>
      <description>Hi
This does the same as your code, but is a little less complicated.

* split multiple anchors in one record per value;
data anchor; set viswin (where=(anchor^='n/a')); 
	do i = 1 to countw(anchor);
		dset=scan(anchor,i); 
		output;
	end;
run;

proc sql noprint; * noprint to avoid output-display from second select;
	* get distinct list;
	select distinct dset into :anchor_list separated by ' '
		from anchor;

	* count;
	select count(distinct(dset)) into: anchor_count
		from anchor;
quit;
%let anchor_count = &amp;amp;anchor_count; * to get rid of blanks;
%put &amp;amp;=anchor_list;
%put &amp;amp;=anchor_count;</description>
      <pubDate>Tue, 20 Feb 2018 16:24:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-a-distinct-list-of-words-in-a-variable/m-p/438699#M109400</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2018-02-20T16:24:21Z</dc:date>
    </item>
    <item>
      <title>Re: How to get a distinct list of words in a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-a-distinct-list-of-words-in-a-variable/m-p/438700#M109401</link>
      <description>&lt;P&gt;Sorry the code seems to be unreadable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;* split multiple anchors in one record per value;
data anchor; set viswin (where=(anchor^='n/a')); 
	do i = 1 to countw(anchor);
		dset=scan(anchor,i); 
		output;
	end;
run;

proc sql noprint; * noprint to avoid output-display from second select;
	* get distinct list;
	select distinct dset into :anchor_list separated by ' '
		from anchor;

	* count;
	select count(distinct(dset)) into: anchor_count
		from anchor;
quit;
%let anchor_count = &amp;amp;anchor_count; * to get rid of blanks;
%put &amp;amp;=anchor_list;
%put &amp;amp;=anchor_count;&lt;/PRE&gt;</description>
      <pubDate>Tue, 20 Feb 2018 16:25:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-a-distinct-list-of-words-in-a-variable/m-p/438700#M109401</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2018-02-20T16:25:58Z</dc:date>
    </item>
    <item>
      <title>Re: How to get a distinct list of words in a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-a-distinct-list-of-words-in-a-variable/m-p/438702#M109403</link>
      <description>This is great. I typically have a hard time going back to some of the basics with loops and output statements. Thank you for your help.</description>
      <pubDate>Tue, 20 Feb 2018 16:28:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-a-distinct-list-of-words-in-a-variable/m-p/438702#M109403</guid>
      <dc:creator>djbateman</dc:creator>
      <dc:date>2018-02-20T16:28:34Z</dc:date>
    </item>
  </channel>
</rss>

