<?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: SAS Turn Row into new Column variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SAS-Turn-Row-into-new-Column-variables/m-p/520659#M141225</link>
    <description>&lt;P&gt;1. Fill in missing values using RETAIN&lt;/P&gt;
&lt;P&gt;2. Transpose by filtering for records that are not screening&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;DATA have;
infile cards truncover;
input @1 name $   @5  stat $    @12 value   @17 Test $;
cards;
Jim Weight 180  Screen
Jim Weight 200  C1
Jim Height 60   Screen
Jim Height 61   C3
Tod Weight 190  Screen
Tod Weight 201  C1
Tod Height 70   Screen
Tod Height      C1
;;;;
proc sort data=have;
by name stat descending test;
run;

data filled;
set have;
by name stat;
retain stat_value;

if first.name or not missing(value) then stat_value = value;

run;


proc transpose data=filled (where = (test ne 'Screen')) 
               out=want;
  by name;
  id stat;
  var stat_value;
run;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/250829"&gt;@serena13lee&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I would like to create a new variable 'Weight' and 'Height' using the variables A through &lt;span class="lia-unicode-emoji" title=":anguished_face:"&gt;😧&lt;/span&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA: 
A     B     C    D 
Jim Weight 180 Screen
Jim Weight 200 C1
Jim Height 60 Screen
Jim Height 61 C3
Tod Weight 190 Screen
Tod Weight 201 C1
Tod Height 70 Screen
Tod Height C1&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The Weight variable would have the following criteria: Set Weight to Column C if Column B = Weight, Column D = C1 and Column C is not missing. Else if Column D is not C1 or Column C is missing, then use the Column C where Column D is Screen. So in plain terms, let's say Jim was weighed during screening and not for C1 then I would like to keep his screen weight. Or if he was screened at C1 but the wight is missing, I would like to keep his screen weight. Similarly for the Height variable.&lt;/P&gt;
&lt;P&gt;My code which is incorrect is:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA MYTEST; 
SET TEST.TEST; 
if B = 'WEIGHT' and D = 'C1D1' and not missing(C) then NEW = C;
else if (missing(C) or D ~= 'C1D1') and B = 'WEIGHT' then WEIGHT = C where D = 'Screen';
if B = 'HEIGHT' and D = 'C1D1' and not missing(C) then NEW = C;
else if (missing(C) or D ~= 'C1D1') and B = 'HEIGHT' then WEIGHT = C where D = 'Screen';
else WEIGHT = 'NA';
RUN; 
PROC PRINT DATA = MYTEST; 
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Desired Outcome: 
DATA: 
A Weight Height 
Jim 200 60
Tod 201 70&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 12 Dec 2018 02:45:24 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2018-12-12T02:45:24Z</dc:date>
    <item>
      <title>SAS Turn Row into new Column variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Turn-Row-into-new-Column-variables/m-p/520644#M141220</link>
      <description>&lt;P&gt;I would like to create a new variable 'Weight' and 'Height' using the variables A through &lt;span class="lia-unicode-emoji" title=":anguished_face:"&gt;😧&lt;/span&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA: 
A     B     C    D 
Jim Weight 180 Screen
Jim Weight 200 C1
Jim Height 60 Screen
Jim Height 61 C3
Tod Weight 190 Screen
Tod Weight 201 C1
Tod Height 70 Screen
Tod Height C1&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The Weight variable would have the following criteria: Set Weight to Column C if Column B = Weight, Column D = C1 and Column C is not missing. Else if Column D is not C1 or Column C is missing, then use the Column C where Column D is Screen. So in plain terms, let's say Jim was weighed during screening and not for C1 then I would like to keep his screen weight. Or if he was screened at C1 but the wight is missing, I would like to keep his screen weight. Similarly for the Height variable.&lt;/P&gt;&lt;P&gt;My code which is incorrect is:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA MYTEST; 
SET TEST.TEST; 
if B = 'WEIGHT' and D = 'C1D1' and not missing(C) then NEW = C;
else if (missing(C) or D ~= 'C1D1') and B = 'WEIGHT' then WEIGHT = C where D = 'Screen';
if B = 'HEIGHT' and D = 'C1D1' and not missing(C) then NEW = C;
else if (missing(C) or D ~= 'C1D1') and B = 'HEIGHT' then WEIGHT = C where D = 'Screen';
else WEIGHT = 'NA';
RUN; 
PROC PRINT DATA = MYTEST; 
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Desired Outcome: 
DATA: 
A Weight Height 
Jim 200 60
Tod 201 70&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 12 Dec 2018 00:47:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Turn-Row-into-new-Column-variables/m-p/520644#M141220</guid>
      <dc:creator>serena13lee</dc:creator>
      <dc:date>2018-12-12T00:47:00Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Turn Row into new Column variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Turn-Row-into-new-Column-variables/m-p/520659#M141225</link>
      <description>&lt;P&gt;1. Fill in missing values using RETAIN&lt;/P&gt;
&lt;P&gt;2. Transpose by filtering for records that are not screening&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;DATA have;
infile cards truncover;
input @1 name $   @5  stat $    @12 value   @17 Test $;
cards;
Jim Weight 180  Screen
Jim Weight 200  C1
Jim Height 60   Screen
Jim Height 61   C3
Tod Weight 190  Screen
Tod Weight 201  C1
Tod Height 70   Screen
Tod Height      C1
;;;;
proc sort data=have;
by name stat descending test;
run;

data filled;
set have;
by name stat;
retain stat_value;

if first.name or not missing(value) then stat_value = value;

run;


proc transpose data=filled (where = (test ne 'Screen')) 
               out=want;
  by name;
  id stat;
  var stat_value;
run;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/250829"&gt;@serena13lee&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I would like to create a new variable 'Weight' and 'Height' using the variables A through &lt;span class="lia-unicode-emoji" title=":anguished_face:"&gt;😧&lt;/span&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA: 
A     B     C    D 
Jim Weight 180 Screen
Jim Weight 200 C1
Jim Height 60 Screen
Jim Height 61 C3
Tod Weight 190 Screen
Tod Weight 201 C1
Tod Height 70 Screen
Tod Height C1&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The Weight variable would have the following criteria: Set Weight to Column C if Column B = Weight, Column D = C1 and Column C is not missing. Else if Column D is not C1 or Column C is missing, then use the Column C where Column D is Screen. So in plain terms, let's say Jim was weighed during screening and not for C1 then I would like to keep his screen weight. Or if he was screened at C1 but the wight is missing, I would like to keep his screen weight. Similarly for the Height variable.&lt;/P&gt;
&lt;P&gt;My code which is incorrect is:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA MYTEST; 
SET TEST.TEST; 
if B = 'WEIGHT' and D = 'C1D1' and not missing(C) then NEW = C;
else if (missing(C) or D ~= 'C1D1') and B = 'WEIGHT' then WEIGHT = C where D = 'Screen';
if B = 'HEIGHT' and D = 'C1D1' and not missing(C) then NEW = C;
else if (missing(C) or D ~= 'C1D1') and B = 'HEIGHT' then WEIGHT = C where D = 'Screen';
else WEIGHT = 'NA';
RUN; 
PROC PRINT DATA = MYTEST; 
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Desired Outcome: 
DATA: 
A Weight Height 
Jim 200 60
Tod 201 70&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Dec 2018 02:45:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Turn-Row-into-new-Column-variables/m-p/520659#M141225</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-12-12T02:45:24Z</dc:date>
    </item>
  </channel>
</rss>

