<?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: Looking through a row and returning a value from a range of columns in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Looking-through-a-row-and-returning-a-value-from-a-range-of/m-p/250898#M56642</link>
    <description>&lt;P&gt;The following code choose the first '1' if you have multiple, tweak it to accomodate your real data&amp;amp;colon;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Obs ID A B C ;
cards;
1 1 1 . . 
2 1 . 1 . 
3 1 1 . . 
;

data want;
set have;
array _t a--c;
length category $ 10;
category=vname(_t(whichn(1,of _t(*))));
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 18 Feb 2016 17:38:06 GMT</pubDate>
    <dc:creator>Haikuo</dc:creator>
    <dc:date>2016-02-18T17:38:06Z</dc:date>
    <item>
      <title>Looking through a row and returning a value from a range of columns</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Looking-through-a-row-and-returning-a-value-from-a-range-of/m-p/250885#M56636</link>
      <description>&lt;P&gt;Hi,&lt;BR /&gt;&lt;BR /&gt;I have the following dataset:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;STRONG&gt;Obs&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;ID&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;A&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;B&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;C&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;How do I make SAS look through columns A, B and C for each observation, to return a table as below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;STRONG&gt;Obs&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;ID&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;A&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;B&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;C&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;Category&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Thu, 18 Feb 2016 16:55:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Looking-through-a-row-and-returning-a-value-from-a-range-of/m-p/250885#M56636</guid>
      <dc:creator>udden2903</dc:creator>
      <dc:date>2016-02-18T16:55:18Z</dc:date>
    </item>
    <item>
      <title>Re: Looking through a row and returning a value from a range of columns</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Looking-through-a-row-and-returning-a-value-from-a-range-of/m-p/250892#M56639</link>
      <description>&lt;P&gt;Let's clarify the problem a little. Are you looking for the "first" variable of A B or C with ANY value or specifically the first with a value of 1?&lt;/P&gt;
&lt;P&gt;What do you want for output if all of the values are missing?&lt;/P&gt;</description>
      <pubDate>Thu, 18 Feb 2016 17:22:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Looking-through-a-row-and-returning-a-value-from-a-range-of/m-p/250892#M56639</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-02-18T17:22:40Z</dc:date>
    </item>
    <item>
      <title>Re: Looking through a row and returning a value from a range of columns</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Looking-through-a-row-and-returning-a-value-from-a-range-of/m-p/250895#M56640</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
   input id:$1. A B C;
cards;
1 1 . .
1 . 1 .
1 1 . .
;
run;
data want(drop=str key lastcat);
format key $8. category $1.;
if _n_ = 1 then do;
	lastCat = 0;
	declare hash lu();
	lu.defineKey('key');
	lu.defineData('category');
	lu.defineDone();
end;
set have;
array vars {3} A B C;
str = 'ABCDFGHIJKLMNOPQRSTUVWXYZ';

key=catx(',',of vars{*});
if lu.find()=0 then output;
else do;
	lastCat + 1;
	category = substr(str,lastCat,1);
	lu.add();
	output;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The question of what the category value should be is a little tricky. If it has to be a letter, then this code will work for up to 26 categories.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It would be easiet if the category values were numbers, in which case you would just change category = lastCat instead of substr(), and remove the str variable creation.&lt;/P&gt;</description>
      <pubDate>Thu, 18 Feb 2016 18:12:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Looking-through-a-row-and-returning-a-value-from-a-range-of/m-p/250895#M56640</guid>
      <dc:creator>DanZ</dc:creator>
      <dc:date>2016-02-18T18:12:23Z</dc:date>
    </item>
    <item>
      <title>Re: Looking through a row and returning a value from a range of columns</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Looking-through-a-row-and-returning-a-value-from-a-range-of/m-p/250898#M56642</link>
      <description>&lt;P&gt;The following code choose the first '1' if you have multiple, tweak it to accomodate your real data&amp;amp;colon;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Obs ID A B C ;
cards;
1 1 1 . . 
2 1 . 1 . 
3 1 1 . . 
;

data want;
set have;
array _t a--c;
length category $ 10;
category=vname(_t(whichn(1,of _t(*))));
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 18 Feb 2016 17:38:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Looking-through-a-row-and-returning-a-value-from-a-range-of/m-p/250898#M56642</guid>
      <dc:creator>Haikuo</dc:creator>
      <dc:date>2016-02-18T17:38:06Z</dc:date>
    </item>
    <item>
      <title>Re: Looking through a row and returning a value from a range of columns</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Looking-through-a-row-and-returning-a-value-from-a-range-of/m-p/250934#M56643</link>
      <description>Hi!&lt;BR /&gt;&lt;BR /&gt;A, B and C are dummy variables. In the original dataset, each ID represents a unique patient, while A, B and C are different diagnoses.&lt;BR /&gt;&lt;BR /&gt;I want to insert an additional column where the patient's diagnosis is given as a string variable. For example, if patient 1 had a value of 1 for the diagnosis B dummy, then I want a new variable that says B. If a patient has missing values for every dummy variable A, B and C, then I want the new variable to show missing as well. Similarly, if they had any combination of A, B and C, I want the new variable to show a combination: For example, if patient 1 has a value of 1 for A and B, then I want my new variable to say AB.</description>
      <pubDate>Thu, 18 Feb 2016 20:03:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Looking-through-a-row-and-returning-a-value-from-a-range-of/m-p/250934#M56643</guid>
      <dc:creator>udden2903</dc:creator>
      <dc:date>2016-02-18T20:03:15Z</dc:date>
    </item>
    <item>
      <title>Re: Looking through a row and returning a value from a range of columns</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Looking-through-a-row-and-returning-a-value-from-a-range-of/m-p/250935#M56644</link>
      <description>Hi!&lt;BR /&gt;&lt;BR /&gt;The categories refer to different diagnoses in my original dataset. These are made up of one letter followed by two digits, e.g. L40, M06, K50.</description>
      <pubDate>Thu, 18 Feb 2016 20:06:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Looking-through-a-row-and-returning-a-value-from-a-range-of/m-p/250935#M56644</guid>
      <dc:creator>udden2903</dc:creator>
      <dc:date>2016-02-18T20:06:00Z</dc:date>
    </item>
    <item>
      <title>Re: Looking through a row and returning a value from a range of columns</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Looking-through-a-row-and-returning-a-value-from-a-range-of/m-p/250937#M56645</link>
      <description>Hi!&lt;BR /&gt;&lt;BR /&gt;In my original dataset, an observation can have a value of 1 in more than one of the columns. In that case, I need SAS to return the name of both columns where the observation has a value of 1.</description>
      <pubDate>Thu, 18 Feb 2016 20:11:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Looking-through-a-row-and-returning-a-value-from-a-range-of/m-p/250937#M56645</guid>
      <dc:creator>udden2903</dc:creator>
      <dc:date>2016-02-18T20:11:40Z</dc:date>
    </item>
    <item>
      <title>Re: Looking through a row and returning a value from a range of columns</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Looking-through-a-row-and-returning-a-value-from-a-range-of/m-p/250939#M56646</link>
      <description>Do you require the category values to be letters?</description>
      <pubDate>Thu, 18 Feb 2016 20:18:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Looking-through-a-row-and-returning-a-value-from-a-range-of/m-p/250939#M56646</guid>
      <dc:creator>DanZ</dc:creator>
      <dc:date>2016-02-18T20:18:06Z</dc:date>
    </item>
    <item>
      <title>Re: Looking through a row and returning a value from a range of columns</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Looking-through-a-row-and-returning-a-value-from-a-range-of/m-p/250942#M56648</link>
      <description>No, I only need to be able to categorize the observations based on diagnosis.</description>
      <pubDate>Thu, 18 Feb 2016 20:24:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Looking-through-a-row-and-returning-a-value-from-a-range-of/m-p/250942#M56648</guid>
      <dc:creator>udden2903</dc:creator>
      <dc:date>2016-02-18T20:24:17Z</dc:date>
    </item>
    <item>
      <title>Re: Looking through a row and returning a value from a range of columns</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Looking-through-a-row-and-returning-a-value-from-a-range-of/m-p/250944#M56650</link>
      <description>And, I'm assuming that you're not concerned with the order of the diagnosis? Ie. the variables A and B with values L01/M04 would be the same as M04/L01.</description>
      <pubDate>Thu, 18 Feb 2016 20:26:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Looking-through-a-row-and-returning-a-value-from-a-range-of/m-p/250944#M56650</guid>
      <dc:creator>DanZ</dc:creator>
      <dc:date>2016-02-18T20:26:05Z</dc:date>
    </item>
    <item>
      <title>Re: Looking through a row and returning a value from a range of columns</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Looking-through-a-row-and-returning-a-value-from-a-range-of/m-p/250945#M56651</link>
      <description>The variables A and B are dummy variables. In my original dataset, these variables have names like L01 and M04, but the actual values are either 1 or "."</description>
      <pubDate>Thu, 18 Feb 2016 20:28:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Looking-through-a-row-and-returning-a-value-from-a-range-of/m-p/250945#M56651</guid>
      <dc:creator>udden2903</dc:creator>
      <dc:date>2016-02-18T20:28:54Z</dc:date>
    </item>
    <item>
      <title>Re: Looking through a row and returning a value from a range of columns</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Looking-through-a-row-and-returning-a-value-from-a-range-of/m-p/250953#M56654</link>
      <description>&lt;P&gt;Here, after a minor tweak,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Obs ID A B C ;
cards;
1 1 1 . . 
2 1 . 1 . 
3 1 1 . 1 
;

data want;
set have;
array _t a--c;
length category $ 100;
do over _t;
if _t=1 then category=catx('-',category,vname(_t));
/*category=vname(_t(whichn(1,of _t(*))));*/
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 18 Feb 2016 20:48:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Looking-through-a-row-and-returning-a-value-from-a-range-of/m-p/250953#M56654</guid>
      <dc:creator>Haikuo</dc:creator>
      <dc:date>2016-02-18T20:48:07Z</dc:date>
    </item>
    <item>
      <title>Re: Looking through a row and returning a value from a range of columns</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Looking-through-a-row-and-returning-a-value-from-a-range-of/m-p/250957#M56655</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
   input 
		id:$1. 
		M01 : 1. 
		M02 : 1. 
		L01 : 1.;
cards;
A 1 . .
B 1 . 1
C 1 . . 
D . 1 1
E 1 . 1
;
run;
data want(drop=key lastcat);
format key $60.;
if _n_ = 1 then do;
	lastCat = 0;
	declare hash lu();
	lu.defineKey('key');
	lu.defineData('category');
	lu.defineDone();
end;
set have;
array vars {3} M01 -- L01;
key=catQ('a',',',of vars{*});
if lu.find()=0 then output;
else do;
	lastCat +1;
	category = lastCat;
	lu.add();
	output;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Ok, I think this is what you're describing.&lt;/P&gt;</description>
      <pubDate>Thu, 18 Feb 2016 20:50:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Looking-through-a-row-and-returning-a-value-from-a-range-of/m-p/250957#M56655</guid>
      <dc:creator>DanZ</dc:creator>
      <dc:date>2016-02-18T20:50:18Z</dc:date>
    </item>
    <item>
      <title>Re: Looking through a row and returning a value from a range of columns</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Looking-through-a-row-and-returning-a-value-from-a-range-of/m-p/251049#M56663</link>
      <description>This solved the problem. Many thanks for your help!</description>
      <pubDate>Fri, 19 Feb 2016 08:07:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Looking-through-a-row-and-returning-a-value-from-a-range-of/m-p/251049#M56663</guid>
      <dc:creator>udden2903</dc:creator>
      <dc:date>2016-02-19T08:07:16Z</dc:date>
    </item>
  </channel>
</rss>

