<?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 Conditionally populate all columns based on category in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-populate-all-columns-based-on-category/m-p/550346#M152793</link>
    <description>&lt;P&gt;Hi SAS community,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a dataset that has different categories. For each row it will tell which category the observation belongs to (In this example I am using &lt;STRONG&gt;cat1&lt;/STRONG&gt;,&amp;nbsp;&lt;STRONG&gt;cat2, &lt;/STRONG&gt;and &lt;STRONG&gt;cat3&lt;/STRONG&gt;). Based on what category, I need to pick the variables that correspond to that category. Then I need to put them all into one combined category that has the first to the nth variable.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data&amp;nbsp;have1;
input&amp;nbsp;id&amp;nbsp;model $ a b c d e;
datalines;
1 cat1 1 2 3 4 5
2 cat2 2 3 4 5 6&lt;BR /&gt;3 cat3 3 4 5 6 7
;
run;&lt;BR /&gt;%let cat1= a b c; &lt;BR /&gt;%let cat2= b d e;&lt;BR /&gt;%let cat3= e;&lt;/PRE&gt;&lt;P&gt;Now what I want a final product is;&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;ID model First Second Third&lt;/P&gt;&lt;P&gt;1&amp;nbsp; cat1&amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&lt;/P&gt;&lt;P&gt;2&amp;nbsp; cat2&amp;nbsp; &amp;nbsp; &amp;nbsp; 3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 6&lt;/P&gt;&lt;P&gt;3&amp;nbsp; cat3&amp;nbsp; &amp;nbsp; 7&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can anyone help me with how I would do this? If you have any questions about what I am trying to do I am happy to help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Best,&lt;/P&gt;&lt;P&gt;Tom&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 11 Apr 2019 17:31:05 GMT</pubDate>
    <dc:creator>Tommy1</dc:creator>
    <dc:date>2019-04-11T17:31:05Z</dc:date>
    <item>
      <title>Conditionally populate all columns based on category</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-populate-all-columns-based-on-category/m-p/550346#M152793</link>
      <description>&lt;P&gt;Hi SAS community,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a dataset that has different categories. For each row it will tell which category the observation belongs to (In this example I am using &lt;STRONG&gt;cat1&lt;/STRONG&gt;,&amp;nbsp;&lt;STRONG&gt;cat2, &lt;/STRONG&gt;and &lt;STRONG&gt;cat3&lt;/STRONG&gt;). Based on what category, I need to pick the variables that correspond to that category. Then I need to put them all into one combined category that has the first to the nth variable.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data&amp;nbsp;have1;
input&amp;nbsp;id&amp;nbsp;model $ a b c d e;
datalines;
1 cat1 1 2 3 4 5
2 cat2 2 3 4 5 6&lt;BR /&gt;3 cat3 3 4 5 6 7
;
run;&lt;BR /&gt;%let cat1= a b c; &lt;BR /&gt;%let cat2= b d e;&lt;BR /&gt;%let cat3= e;&lt;/PRE&gt;&lt;P&gt;Now what I want a final product is;&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;ID model First Second Third&lt;/P&gt;&lt;P&gt;1&amp;nbsp; cat1&amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&lt;/P&gt;&lt;P&gt;2&amp;nbsp; cat2&amp;nbsp; &amp;nbsp; &amp;nbsp; 3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 6&lt;/P&gt;&lt;P&gt;3&amp;nbsp; cat3&amp;nbsp; &amp;nbsp; 7&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can anyone help me with how I would do this? If you have any questions about what I am trying to do I am happy to help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Best,&lt;/P&gt;&lt;P&gt;Tom&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 11 Apr 2019 17:31:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-populate-all-columns-based-on-category/m-p/550346#M152793</guid>
      <dc:creator>Tommy1</dc:creator>
      <dc:date>2019-04-11T17:31:05Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally populate all columns based on category</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-populate-all-columns-based-on-category/m-p/550355#M152798</link>
      <description>&lt;P&gt;Let's leave macro variables out, and instead put the information you had in a macro variable into data set variables. Then call in the poorly understood and difficult to pronounce VVALUEX function.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data b;
infile cards missover;
input model $ firstletter $ secondletter $ thirdletter $;
datalines;
cat1 a b c
cat2 b d e
cat3 e
;
run;

/* You may need PROC SORT in here in real life to do the next MERGE */
data both;
    merge have1 b;
    by model;
    first = vvaluex(firstletter);
    second = vvaluex (secondletter);
    third = vvaluex(thirdletter);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 11 Apr 2019 17:57:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-populate-all-columns-based-on-category/m-p/550355#M152798</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-04-11T17:57:00Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally populate all columns based on category</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-populate-all-columns-based-on-category/m-p/550780#M152946</link>
      <description>&lt;P&gt;Hi Paige, thanks for the reply!&amp;nbsp; I have never heard about that function and that is really interesting.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am a little confused how to do this still. When I first expressed the question I don't think I represented my problem as well as I would have liked and I apologize for wasting your time. I thought that I could simplify the problem to figure it out, but it left me more confused.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In my data there are actually two data sets. the first data set has responses to questions. In the example that I provided, the responses are the variables represented by&amp;nbsp;&lt;STRONG&gt;a&lt;/STRONG&gt;-&lt;STRONG&gt;e&lt;/STRONG&gt;. In the second dataset, each row has a different model denoted by &lt;STRONG&gt;model &lt;/STRONG&gt;in the example. Each model corresponds to a separate list of questions (ex. &lt;STRONG&gt;cat1&lt;/STRONG&gt;&amp;nbsp;in dataset two may correspond to questions &lt;STRONG&gt;a&lt;/STRONG&gt;, &lt;STRONG&gt;b&lt;/STRONG&gt;, and &lt;STRONG&gt;c&amp;nbsp;&lt;/STRONG&gt;in dataset one). In my example I tried combining the steps. Using the method that you suggested, I was able to create fields &lt;STRONG&gt;Val1-Val8&amp;nbsp;&lt;/STRONG&gt;in dataset2 that represent the questions that are needed for each category. So if I were to continue my example my data is actually better represented by the example below. My goal is to get the answers from data1 to data2 based on the corresponding model&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data&amp;nbsp;data1;
input&amp;nbsp;id mon year a b c d e;
datalines;
1 1 2009 1 2 3 4 5
2 1 2009 2 3 4 5 6&lt;BR /&gt;3 1 2009 3 4 5 6 7
;
run;&lt;BR /&gt;&lt;BR /&gt;data data2;&lt;BR /&gt;input id mon year model $  val1 $ val2 $ val3 $ val4 $ val5 $; &lt;BR /&gt;datalines;&lt;BR /&gt;1 1 2009 cat1 a  c  e&lt;BR /&gt;2 1 2009 cat2 b  d  e&lt;BR /&gt;3 1 2009 cat3 e&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp; What I need is to match up based on id and monthend and get a final dataset that would look like&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;id mon year model&amp;nbsp; val1&amp;nbsp; val2&amp;nbsp; val3&amp;nbsp; val4&amp;nbsp; val5 q1 q2 q3 q4 q5&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp;2009 cat1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; a&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;c&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; e&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp;3&amp;nbsp; &amp;nbsp; 5&lt;BR /&gt;2&amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; 2009 cat2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;b&amp;nbsp; &amp;nbsp; &amp;nbsp; d&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;e&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3&amp;nbsp; 5&amp;nbsp; &amp;nbsp; 6&lt;BR /&gt;3&amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp;2009 cat3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;e&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;7&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;Thanks for the help,&lt;/P&gt;&lt;P&gt;Tom&lt;/P&gt;</description>
      <pubDate>Fri, 12 Apr 2019 20:50:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-populate-all-columns-based-on-category/m-p/550780#M152946</guid>
      <dc:creator>Tommy1</dc:creator>
      <dc:date>2019-04-12T20:50:57Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally populate all columns based on category</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-populate-all-columns-based-on-category/m-p/550836#M152972</link>
      <description>&lt;P&gt;The role of the variable "model" is not clear. It seems that you need to merge by id to solve the issue:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data data2;
infile datalines missover; /* added missover option */
input id mon year model $ val1 $ val2 $ val3 $ val4 $ val5 $; 
datalines;
1 1 2009 cat1 a  c  e
2 1 2009 cat2 b  d  e
3 1 2009 cat3 e
;
run;

data work.want;
    merge work.data1 work.data2;
    by id;
    
    length q1-q5 8;
    
    array values[5] val1-val5;
    array questions[5] q1-q5;
    
    do _i = 1 to 5;
        if not missing(values[_i]) then do;
            questions[_i] = vvaluex(values[_i]);
        end;
    end;
    
    drop _i a b c d e;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 13 Apr 2019 09:59:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-populate-all-columns-based-on-category/m-p/550836#M152972</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2019-04-13T09:59:56Z</dc:date>
    </item>
  </channel>
</rss>

