<?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: Adding a new variable for each value of variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Adding-a-new-variable-for-each-value-of-variable/m-p/682330#M206533</link>
    <description>&lt;P&gt;This one will likely work as well and includes setting the values to 0 if missing and 1 if not.&lt;/P&gt;</description>
    <pubDate>Tue, 08 Sep 2020 17:26:56 GMT</pubDate>
    <dc:creator>learningsas101</dc:creator>
    <dc:date>2020-09-08T17:26:56Z</dc:date>
    <item>
      <title>Adding a new variable for each value of variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-a-new-variable-for-each-value-of-variable/m-p/682267#M206512</link>
      <description>&lt;P&gt;I have a variable that has about 50 potential values which have no prefix/suffix or any pattern in them.&amp;nbsp; I need to create a variable named that value and set the new variable to 1 with the other 49 new variables in that row to be set to 0.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is a small sample of my current data set.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="learningsas101_0-1599577342440.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/49099i0DA960D064CBB498/image-size/medium?v=v2&amp;amp;px=400" role="button" title="learningsas101_0-1599577342440.png" alt="learningsas101_0-1599577342440.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;Here is what I want.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="learningsas101_1-1599577363953.png" style="width: 790px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/49100i7CE1A876638327B0/image-dimensions/790x214?v=v2" width="790" height="214" role="button" title="learningsas101_1-1599577363953.png" alt="learningsas101_1-1599577363953.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Sep 2020 15:03:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-a-new-variable-for-each-value-of-variable/m-p/682267#M206512</guid>
      <dc:creator>learningsas101</dc:creator>
      <dc:date>2020-09-08T15:03:20Z</dc:date>
    </item>
    <item>
      <title>Re: Adding a new variable for each value of variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-a-new-variable-for-each-value-of-variable/m-p/682269#M206513</link>
      <description>&lt;P&gt;Why?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Not trying to cause problems but this sort of dummy variable is often added to do certain forms of regression and as such are generally not needed as a Class variable behaves as these dummies would.&lt;/P&gt;</description>
      <pubDate>Tue, 08 Sep 2020 15:07:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-a-new-variable-for-each-value-of-variable/m-p/682269#M206513</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-09-08T15:07:04Z</dc:date>
    </item>
    <item>
      <title>Re: Adding a new variable for each value of variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-a-new-variable-for-each-value-of-variable/m-p/682271#M206514</link>
      <description>&lt;P&gt;First of all: do not post pictures of data. To supply example data, use a data step with datalines which you post into a code box opened with the "little running man" button right next to the one indicated:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Bildschirmfoto 2020-04-07 um 08.32.59.jpg"&gt;&lt;img src="https://communities.sas.com/skins/images/70F8802BAA6255D55FBEC62A8226FB10/responsive_peak/images/image_not_found.png" alt="Bildschirmfoto 2020-04-07 um 08.32.59.jpg" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;(the indicated button is for logs and other textual data where formatting is critical).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your issue looks like a task for proc transpose, but in your case you only create redundant information; what are you trying to achieve in the end?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Sep 2020 15:11:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-a-new-variable-for-each-value-of-variable/m-p/682271#M206514</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-09-08T15:11:19Z</dc:date>
    </item>
    <item>
      <title>Re: Adding a new variable for each value of variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-a-new-variable-for-each-value-of-variable/m-p/682273#M206515</link>
      <description>&lt;P&gt;I tried, but for whatever reason, it would allow it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to create counters for a data set.&amp;nbsp; I've tried proc transpose, but it doesn't seem to get me where I want to be.&amp;nbsp; I'm trying to avoid having to write 50 if/then statements.&amp;nbsp; Perhaps something with Proc Transpose and then a do loop of some sort?&lt;/P&gt;</description>
      <pubDate>Tue, 08 Sep 2020 15:23:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-a-new-variable-for-each-value-of-variable/m-p/682273#M206515</guid>
      <dc:creator>learningsas101</dc:creator>
      <dc:date>2020-09-08T15:23:20Z</dc:date>
    </item>
    <item>
      <title>Re: Adding a new variable for each value of variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-a-new-variable-for-each-value-of-variable/m-p/682282#M206516</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp / view=temp;
set have;
count=1;
run;

proc transpose data=have out=wide prefix=State_;
by area name;
id type_code;
var count;
run;

data want;
set wide;
array _state(*) state_;
do i=1 to dim(_state);
    if missing(_state(i)) then _state(i) = 0;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Fully dynamic above.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Alternatively:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
select distinct type_code into : code_list separated by " "
from have;
quit;

data want;
set have;

array states(*) &amp;amp;code_list.; *list your codes manually;

do i=1 to dim(states);
if vname(states(i)) = type_cd then states(i) = 1;
else states(i) = 0;
end;

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Sep 2020 15:40:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-a-new-variable-for-each-value-of-variable/m-p/682282#M206516</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-09-08T15:40:03Z</dc:date>
    </item>
    <item>
      <title>Re: Adding a new variable for each value of variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-a-new-variable-for-each-value-of-variable/m-p/682283#M206517</link>
      <description>&lt;P&gt;Please try below code&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input type_cd$	area 	name$;
cards;
AZ 3  Lucy
BH 5  Frank
CR 1  John
IM 8  Susan
KS 10 Mary
LL 9  Jill
;

proc sql noprint; 
select distinct type_cd into: valv separated by ' ' from have;
quit;


data want;
set have;
array vals(&amp;amp;sqlobs)  &amp;amp;valv;
do i = 1 to &amp;amp;sqlobs;
if vname(vals(i))=type_cd then vals(i)=1;
else vals(i)=0;
end;
drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 08 Sep 2020 15:45:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-a-new-variable-for-each-value-of-variable/m-p/682283#M206517</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2020-09-08T15:45:15Z</dc:date>
    </item>
    <item>
      <title>Re: Adding a new variable for each value of variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-a-new-variable-for-each-value-of-variable/m-p/682284#M206518</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/161583"&gt;@learningsas101&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I tried, but for whatever reason, it would allow it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm trying to create counters for a data set.&amp;nbsp; I've tried proc transpose, but it doesn't seem to get me where I want to be.&amp;nbsp; I'm trying to avoid having to write 50 if/then statements.&amp;nbsp; Perhaps something with Proc Transpose and then a do loop of some sort?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Count what exactly? Or what kind of counters?&lt;/P&gt;
&lt;P&gt;If it is levels of Type_cd then proc freq will count that quite handily.&lt;/P&gt;</description>
      <pubDate>Tue, 08 Sep 2020 15:45:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-a-new-variable-for-each-value-of-variable/m-p/682284#M206518</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-09-08T15:45:40Z</dc:date>
    </item>
    <item>
      <title>Re: Adding a new variable for each value of variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-a-new-variable-for-each-value-of-variable/m-p/682289#M206521</link>
      <description>&lt;P&gt;I think this might work.&amp;nbsp; I'll give it a go and let you know.&lt;/P&gt;</description>
      <pubDate>Tue, 08 Sep 2020 15:54:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-a-new-variable-for-each-value-of-variable/m-p/682289#M206521</guid>
      <dc:creator>learningsas101</dc:creator>
      <dc:date>2020-09-08T15:54:47Z</dc:date>
    </item>
    <item>
      <title>Re: Adding a new variable for each value of variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-a-new-variable-for-each-value-of-variable/m-p/682330#M206533</link>
      <description>&lt;P&gt;This one will likely work as well and includes setting the values to 0 if missing and 1 if not.&lt;/P&gt;</description>
      <pubDate>Tue, 08 Sep 2020 17:26:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-a-new-variable-for-each-value-of-variable/m-p/682330#M206533</guid>
      <dc:creator>learningsas101</dc:creator>
      <dc:date>2020-09-08T17:26:56Z</dc:date>
    </item>
    <item>
      <title>Re: Adding a new variable for each value of variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-a-new-variable-for-each-value-of-variable/m-p/682331#M206534</link>
      <description>&lt;P&gt;Proc freq won't work for what I want.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Two others have already posted solutions that will likely work.&amp;nbsp; I'll test them out today or tomorrow and let everyone know.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Sep 2020 17:28:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-a-new-variable-for-each-value-of-variable/m-p/682331#M206534</guid>
      <dc:creator>learningsas101</dc:creator>
      <dc:date>2020-09-08T17:28:14Z</dc:date>
    </item>
    <item>
      <title>Re: Adding a new variable for each value of variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-a-new-variable-for-each-value-of-variable/m-p/682378#M206546</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/161583"&gt;@learningsas101&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Proc freq won't work for what I want.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Two others have already posted solutions that will likely work.&amp;nbsp; I'll test them out today or tomorrow and let everyone know.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;But you haven't described what you are doing.&lt;/P&gt;
&lt;P&gt;When I see 15 variables with values of 0/1 I do not see any "counting" going on.&lt;/P&gt;</description>
      <pubDate>Tue, 08 Sep 2020 19:17:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-a-new-variable-for-each-value-of-variable/m-p/682378#M206546</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-09-08T19:17:37Z</dc:date>
    </item>
  </channel>
</rss>

