<?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: Creating new variables from manifestations of a string variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-from-manifestations-of-a-string-variable/m-p/244231#M268498</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data specialty;
input  ID  specialty $12. ;
x = 1;
cards;
1 Neurologist
1 Psychologist
2 GP
3 Neurologist
4 Cardiologist
5 Cardiologist
;
run;
proc transpose data=specialty out=wide (drop=_NAME_);
by ID;
id specialty;
var x;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This basically provides what you wanted (having missing values instead of zero), but you only get 1 observation for ID=1, where both columns Neuroloigist and Psychologist have the value 1.&lt;/P&gt;</description>
    <pubDate>Mon, 18 Jan 2016 15:01:47 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2016-01-18T15:01:47Z</dc:date>
    <item>
      <title>Creating new variables from manifestations of a string variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-from-manifestations-of-a-string-variable/m-p/244224#M268497</link>
      <description>&lt;P&gt;Hello everyone,&lt;/P&gt;&lt;P&gt;I’m new to SAS and have been trying to figure out how to solve the following problem:&lt;/P&gt;&lt;P&gt;Let’s assume I have the following dataset:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;data&lt;/STRONG&gt; specialty;&lt;/P&gt;&lt;P&gt;&amp;nbsp; input&amp;nbsp; ID&amp;nbsp; specialty $12. ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; cards;&lt;/P&gt;&lt;P&gt;1 Neurologist&lt;/P&gt;&lt;P&gt;1 Psychologist&lt;/P&gt;&lt;P&gt;2 GP&lt;/P&gt;&lt;P&gt;3 Neurologist&lt;/P&gt;&lt;P&gt;4 Cardiologist&lt;/P&gt;&lt;P&gt;5 Cardiologist&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I want to do now is to create a new column for every physician specialty from the column “specialty” and put a “1” in the respective column if it fits and zero otherwise.&lt;/P&gt;&lt;P&gt;So the result should look like this:&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;ID&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;specialty&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;Neurologist&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;Psychologist&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;GP&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;Cardiologist&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;1&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;Neurologist&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;1&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;0&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;0&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;0&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;1&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;Psychologist&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;0&lt;/P&gt;&lt;/TD&gt;&lt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&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;0&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;0&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;0&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;1&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I solved the problem with creating every variable manually and lots of “if then statements” for now but I’m not very happy with this. Is there a more elaborated way to achieve the desired outcome?&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Mon, 18 Jan 2016 14:37:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-from-manifestations-of-a-string-variable/m-p/244224#M268497</guid>
      <dc:creator>D1</dc:creator>
      <dc:date>2016-01-18T14:37:44Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variables from manifestations of a string variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-from-manifestations-of-a-string-variable/m-p/244231#M268498</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data specialty;
input  ID  specialty $12. ;
x = 1;
cards;
1 Neurologist
1 Psychologist
2 GP
3 Neurologist
4 Cardiologist
5 Cardiologist
;
run;
proc transpose data=specialty out=wide (drop=_NAME_);
by ID;
id specialty;
var x;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This basically provides what you wanted (having missing values instead of zero), but you only get 1 observation for ID=1, where both columns Neuroloigist and Psychologist have the value 1.&lt;/P&gt;</description>
      <pubDate>Mon, 18 Jan 2016 15:01:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-from-manifestations-of-a-string-variable/m-p/244231#M268498</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-01-18T15:01:47Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variables from manifestations of a string variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-from-manifestations-of-a-string-variable/m-p/244233#M268499</link>
      <description>&lt;P&gt;Looks like a report to me. What is the intended use for the output data set?&lt;/P&gt;
&lt;P&gt;I believe&amp;nbsp;you can use cross tabulations in PROC FREQ for this.&lt;/P&gt;</description>
      <pubDate>Mon, 18 Jan 2016 15:04:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-from-manifestations-of-a-string-variable/m-p/244233#M268499</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2016-01-18T15:04:12Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variables from manifestations of a string variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-from-manifestations-of-a-string-variable/m-p/244234#M268500</link>
      <description>&lt;P&gt;Well, this will give you what you want:&lt;/P&gt;
&lt;PRE&gt;data specialty;
  input  ID  specialty $12. ;
  res=1;
  cards;
1 Neurologist
1 Psychologist
2 GP
3 Neurologist
4 Cardiologist
5 Cardiologist
;
run;

proc transpose data=specialty out=want;
  by id specialty;
  var res;
  id specialty;
  idlabel specialty;
run;&lt;/PRE&gt;
&lt;P&gt;However I would advise against this approach, if you variables change then your going to need to reprogram. &amp;nbsp;Its generally a good idea to have a general prefix and an incrementor so you can use arrays, you can still label them what ever your like, but its so much easier to code with:&lt;/P&gt;
&lt;PRE&gt;data specialty;
  input  ID  specialty $12. ;
  res=1;
  cards;
1 Neurologist
1 Psychologist
2 GP
3 Neurologist
4 Cardiologist
5 Cardiologist
;
run;

data want;
  set specialty;
  array var{4} 8.;
  select(specialty);
    when ("Neurologist") var{1}=1;
    when ("Psychologist") var{2}=1;
    when ("GP") var{3}=1;
    when ("Cardiologist") var{4}=1;
    otherwise;
  end;
run;&lt;/PRE&gt;
&lt;P&gt;You can then refer to the array of vars in further processing.&lt;/P&gt;</description>
      <pubDate>Mon, 18 Jan 2016 15:05:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-from-manifestations-of-a-string-variable/m-p/244234#M268500</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-01-18T15:05:52Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variables from manifestations of a string variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-from-manifestations-of-a-string-variable/m-p/244239#M268501</link>
      <description>&lt;P&gt;Thank you Kurt and RW9 for the quick responses. That is exactly what I needed!&lt;/P&gt;</description>
      <pubDate>Mon, 18 Jan 2016 15:29:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-from-manifestations-of-a-string-variable/m-p/244239#M268501</guid>
      <dc:creator>D1</dc:creator>
      <dc:date>2016-01-18T15:29:18Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variables from manifestations of a string variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-from-manifestations-of-a-string-variable/m-p/244341#M268502</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data specialty;
input  ID  specialty $12. ;
x = 1;
n+1;
cards;
1 Neurologist
1 Psychologist
2 GP
3 Neurologist
4 Cardiologist
5 Cardiologist
;
run;
proc transpose data=specialty out=wide (drop=_NAME_ n);
by n;
id specialty;
var x;
copy id specialty;
run;
proc stdize data=wide out=want missing=0 reponly;run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 19 Jan 2016 02:30:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-from-manifestations-of-a-string-variable/m-p/244341#M268502</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-01-19T02:30:15Z</dc:date>
    </item>
  </channel>
</rss>

