<?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 create N variables with values 0 and 1 from a string variable containing N different valu in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-N-variables-with-values-0-and-1-from-a-string/m-p/977063#M378418</link>
    <description>&lt;P&gt;Just for fun -- this assumes that:&lt;/P&gt;
&lt;P&gt;1) the total length of your input string variable does not exceed SAS limits (~32K-ish characters?).&lt;/P&gt;
&lt;P&gt;2) the individual values within this string (e.g., 'green', 'red',...) can themselves be legal variable names and that case is consistent&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro maxlen(vals);
	
	%global maxlen unqvals;
	
	%let vals=%cmpres(&amp;amp;vals);
	%let nvals=%sysfunc(countW(&amp;amp;vals, ' '));
	%let maxlen=0;
	%let unqvals=;
	%do i=1 %to &amp;amp;nvals;
		%let val=%scan(&amp;amp;vals,&amp;amp;i,' ');
		%let maxlen=%sysfunc(max(&amp;amp;maxlen, %length(&amp;amp;val)));
		%if %sysfunc(prxmatch(/\b&amp;amp;val\b/i, &amp;amp;unqvals))=0 %then %let unqvals=&amp;amp;unqvals &amp;amp;val;;
	%end;
	
%mend; *maxlen();

%let vals=
	Blue
	Red
	Blue
	Green
	Green
	Yellow
	Green
	Yellow
	;

%maxlen(&amp;amp;vals);

%put &amp;amp;=maxlen;
%put &amp;amp;=unqvals;

data want;
vals=compbl("&amp;amp;vals");
length val $&amp;amp;maxlen &amp;amp;unqvals 3;
array u {*} &amp;amp;unqvals;
do i=1 to &amp;amp;nvals;
	val=scan(vals,i,' ');
	do j=1 to dim(u);
		u[j]=(val=vname(u[j]));
	end;
	output;
end;
keep val &amp;amp;unqvals;
run;

proc print data=want; run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="quickbluefish_0-1760547955844.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/110648i25F3FD2B3EC8319D/image-size/medium?v=v2&amp;amp;px=400" role="button" title="quickbluefish_0-1760547955844.png" alt="quickbluefish_0-1760547955844.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 15 Oct 2025 17:06:01 GMT</pubDate>
    <dc:creator>quickbluefish</dc:creator>
    <dc:date>2025-10-15T17:06:01Z</dc:date>
    <item>
      <title>How to create N variables with values 0 and 1 from a string variable containing N different values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-N-variables-with-values-0-and-1-from-a-string/m-p/977050#M378412</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;is there an easy, preferably automated way to create N variables with values 0 and 1 from a string variable that contains N different values? Ideally the code should work without any knowledge about N or the different values of the original variable.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Example:&lt;/P&gt;&lt;P&gt;Have:&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;COLOR&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Blue&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Red&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Blue&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Green&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Green&lt;/TD&gt;&lt;/TR&gt;&lt;TR&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lt;TR&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Wed, 15 Oct 2025 13:24:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-N-variables-with-values-0-and-1-from-a-string/m-p/977050#M378412</guid>
      <dc:creator>Lanox</dc:creator>
      <dc:date>2025-10-15T13:24:07Z</dc:date>
    </item>
    <item>
      <title>Re: How to create N variables with values 0 and 1 from a string variable containing N different valu</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-N-variables-with-values-0-and-1-from-a-string/m-p/977052#M378413</link>
      <description>&lt;P&gt;What is the purpose of this, what further analysis do you want to do with the wide structure?&lt;/P&gt;</description>
      <pubDate>Wed, 15 Oct 2025 13:33:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-N-variables-with-values-0-and-1-from-a-string/m-p/977052#M378413</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2025-10-15T13:33:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to create N variables with values 0 and 1 from a string variable containing N different valu</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-N-variables-with-values-0-and-1-from-a-string/m-p/977053#M378414</link>
      <description>&lt;P&gt;I think this will work for you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data color;
   id = _n_;
   input color :$12.;
   cards;
Blue
Red
Blue
Green
Green
Yellow
Green
Yellow
;;;;
   run;
proc print;
   run;
proc transreg data=color;
   model class(color/zero='1');
   id id color;
   output out=design(drop=intercept) design;
   run;
proc print; 
   run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG" style="width: 467px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/110647i4646179D657F363C/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 15 Oct 2025 13:39:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-N-variables-with-values-0-and-1-from-a-string/m-p/977053#M378414</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2025-10-15T13:39:09Z</dc:date>
    </item>
    <item>
      <title>Re: How to create N variables with values 0 and 1 from a string variable containing N different valu</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-N-variables-with-values-0-and-1-from-a-string/m-p/977056#M378415</link>
      <description>&lt;P&gt;PROC GLMSELECT can do that.&amp;nbsp; See this &lt;A href="https://blogs.sas.com/content/iml/2020/08/31/best-generate-dummy-variables-sas.html" target="_self"&gt;blog post&lt;/A&gt;.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You need a numeric variable to include in your MODEL statement.&amp;nbsp; If you don't have one you can just use a SUM statement to make one.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  row+1;
  input color $10.;
cards;
Blue
Red
Blue
Green
Green
Yellow
Green
Yellow
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now let's make the new dataset.&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;proc glmselect data=have NOPRINT outdesign(addinputvars)=Want;
   class  color;   /* list the categorical variables here */
   model row = color /  noint selection=none;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;         color_    color_    color_    color_
  Obs     Blue      Green      Red     Yellow    row    color

   1        1         0         0         0       1     Blue
   2        0         0         1         0       2     Red
   3        1         0         0         0       3     Blue
   4        0         1         0         0       4     Green
   5        0         1         0         0       5     Green
   6        0         0         0         1       6     Yellow
   7        0         1         0         0       7     Green
   8        0         0         0         1       8     Yellow
&lt;/PRE&gt;</description>
      <pubDate>Wed, 15 Oct 2025 13:43:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-N-variables-with-values-0-and-1-from-a-string/m-p/977056#M378415</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-10-15T13:43:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to create N variables with values 0 and 1 from a string variable containing N different valu</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-N-variables-with-values-0-and-1-from-a-string/m-p/977063#M378418</link>
      <description>&lt;P&gt;Just for fun -- this assumes that:&lt;/P&gt;
&lt;P&gt;1) the total length of your input string variable does not exceed SAS limits (~32K-ish characters?).&lt;/P&gt;
&lt;P&gt;2) the individual values within this string (e.g., 'green', 'red',...) can themselves be legal variable names and that case is consistent&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro maxlen(vals);
	
	%global maxlen unqvals;
	
	%let vals=%cmpres(&amp;amp;vals);
	%let nvals=%sysfunc(countW(&amp;amp;vals, ' '));
	%let maxlen=0;
	%let unqvals=;
	%do i=1 %to &amp;amp;nvals;
		%let val=%scan(&amp;amp;vals,&amp;amp;i,' ');
		%let maxlen=%sysfunc(max(&amp;amp;maxlen, %length(&amp;amp;val)));
		%if %sysfunc(prxmatch(/\b&amp;amp;val\b/i, &amp;amp;unqvals))=0 %then %let unqvals=&amp;amp;unqvals &amp;amp;val;;
	%end;
	
%mend; *maxlen();

%let vals=
	Blue
	Red
	Blue
	Green
	Green
	Yellow
	Green
	Yellow
	;

%maxlen(&amp;amp;vals);

%put &amp;amp;=maxlen;
%put &amp;amp;=unqvals;

data want;
vals=compbl("&amp;amp;vals");
length val $&amp;amp;maxlen &amp;amp;unqvals 3;
array u {*} &amp;amp;unqvals;
do i=1 to &amp;amp;nvals;
	val=scan(vals,i,' ');
	do j=1 to dim(u);
		u[j]=(val=vname(u[j]));
	end;
	output;
end;
keep val &amp;amp;unqvals;
run;

proc print data=want; run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="quickbluefish_0-1760547955844.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/110648i25F3FD2B3EC8319D/image-size/medium?v=v2&amp;amp;px=400" role="button" title="quickbluefish_0-1760547955844.png" alt="quickbluefish_0-1760547955844.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 15 Oct 2025 17:06:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-N-variables-with-values-0-and-1-from-a-string/m-p/977063#M378418</guid>
      <dc:creator>quickbluefish</dc:creator>
      <dc:date>2025-10-15T17:06:01Z</dc:date>
    </item>
  </channel>
</rss>

