<?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: Arrays, lookup and Transpose in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Arrays-lookup-and-Transpose/m-p/584377#M166445</link>
    <description>&lt;P&gt;Hash enumerate all is far better than Point=&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
 infile datalines truncover;
   input subj $ vis $ TEST1 $ TEST1_C $ TEST2 $ TEST2_C $;
   datalines;
001 A Q1 9 P1 3
001 A Q5 4 P2 6
;
run;



data lookup;
 input source $ source_c $;
datalines;
TEST1 TEST1_C
TEST2 TEST2_C
;
run;

data want;
 set have;
 array t(*) test:;
 array temp(99999) $ _temporary_;
 if _n_=1 then do;
 do _n_=1 to dim(t);
  temp(_n_)=vname(t(_n_));
 end;
 if 0 then set lookup;
  dcl hash H (dataset:'lookup');
  h.definekey  (all:'y');
  h.definedata (all:'y');
  h.definedone ();
  dcl hiter hi('h');
 end;
 do while(hi.next()=0);
  res=t(whichc(source,of temp(*)));
  resn=t(whichc(source_c,of temp(*)));
  output;
 end;
 drop test:;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 27 Aug 2019 21:53:20 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2019-08-27T21:53:20Z</dc:date>
    <item>
      <title>Arrays, lookup and Transpose</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Arrays-lookup-and-Transpose/m-p/584363#M166429</link>
      <description>&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;&amp;nbsp;Input: Have, Lookup Datasets. Transpose&amp;nbsp;Have dataset from wide to &lt;/FONT&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;long.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; data have;
 infile datalines truncover;
   input subj $ vis $ TEST1 $ TEST1_C $ TEST2 $ TEST2_C $;
   datalines;
001 A Q1 9 P1 3
001 A Q5 4 P2 6
;
run;

data lookup;
 input source $ source_c $;
datalines;
TEST1 TEST1_C
TEST2 TEST2_C
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;With the following solution,&amp;nbsp;declaring &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;2&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt; separate arrays and got the output in desired format.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let mvars  = TEST1 TEST2  ;
%let mvarsc = TEST1_C TEST2_C  ;

* declaring the lookup avarsc as separate array;

data want;
  set have;
  length source $11. RES $25 ;
   array avars[*] $ &amp;amp;mvars. ;
   array avarsc[*] &amp;amp;mvarsc.;
      
  do i = 1 to dim(avars);
    source = vname(avars[i]); 
	source_c = vname(avarsc[i]);
	RES = avars[i];
	RESN = input(avarsc[i], ??best.);
    output;
  end;

  drop i &amp;amp;mvars. &amp;amp;mvarsc. ;
run;&lt;/CODE&gt;&lt;/PRE&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="want.png" style="width: 580px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/32060iD8F972F598336227/image-size/large?v=v2&amp;amp;px=999" role="button" title="want.png" alt="want.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How do I improve this by having lookup ?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;for RESN variable: tried to overcome this explicit declaration of lookup using a format &lt;/FONT&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;approach.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;***********************************************
using formats:
************************************;

proc format;
 value $srcf
	TEST1 = 'TEST1_C'
	TEST2 = 'TEST2_C'
;
run;

data want1;
set have;
  length source $11. RES $25 ;
   array avars[*] $ &amp;amp;mvars. ;
      
  do i = 1 to dim(avars);
    source = vname(avars[i]); 
	source_c = put(source, $srcf.);
	RES = avars[i];

	* use the value variable in PDV of source_c ??;
	RESN = input(source_c, ?? best.) ;
    output;
  end;

  drop i &amp;amp;mvars. &amp;amp;mvarsc. ;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;RESN got missing values, as SAS doesn&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'t see this as a variable already in Dataset. How Do I connect these dots of taking the values from variable in PDV.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;RESN = using lookup TEST1 has TEST1_C and then use the value of this variable to get numeric value.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for looking into this. Is this a multilevel lookup or more like &amp;amp;&amp;amp;&amp;amp; in macros.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 27 Aug 2019 20:33:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Arrays-lookup-and-Transpose/m-p/584363#M166429</guid>
      <dc:creator>Rkvi</dc:creator>
      <dc:date>2019-08-27T20:33:34Z</dc:date>
    </item>
    <item>
      <title>Re: Arrays, lookup and Transpose</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Arrays-lookup-and-Transpose/m-p/584376#M166444</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
 infile datalines truncover;
   input subj $ vis $ TEST1 $ TEST1_C $ TEST2 $ TEST2_C $;
   datalines;
001 A Q1 9 P1 3
001 A Q5 4 P2 6
;
run;

data lookup;
 input source $ source_c $;
datalines;
TEST1 TEST1_C
TEST2 TEST2_C
;
run;
data want;
 set have;
 array t(*) test:;
 array temp(99999) $ _temporary_;
 if _n_=1 then do _n_=1 to dim(t);
  temp(_n_)=vname(t(_n_));
 end;
 do i=1 to nobs;
  set lookup point=i nobs=nobs;
  res=t(whichc(source,of temp(*)));
  resn=t(whichc(source_c,of temp(*)));
  output;
 end;
 drop test:;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 27 Aug 2019 21:36:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Arrays-lookup-and-Transpose/m-p/584376#M166444</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-08-27T21:36:49Z</dc:date>
    </item>
    <item>
      <title>Re: Arrays, lookup and Transpose</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Arrays-lookup-and-Transpose/m-p/584377#M166445</link>
      <description>&lt;P&gt;Hash enumerate all is far better than Point=&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
 infile datalines truncover;
   input subj $ vis $ TEST1 $ TEST1_C $ TEST2 $ TEST2_C $;
   datalines;
001 A Q1 9 P1 3
001 A Q5 4 P2 6
;
run;



data lookup;
 input source $ source_c $;
datalines;
TEST1 TEST1_C
TEST2 TEST2_C
;
run;

data want;
 set have;
 array t(*) test:;
 array temp(99999) $ _temporary_;
 if _n_=1 then do;
 do _n_=1 to dim(t);
  temp(_n_)=vname(t(_n_));
 end;
 if 0 then set lookup;
  dcl hash H (dataset:'lookup');
  h.definekey  (all:'y');
  h.definedata (all:'y');
  h.definedone ();
  dcl hiter hi('h');
 end;
 do while(hi.next()=0);
  res=t(whichc(source,of temp(*)));
  resn=t(whichc(source_c,of temp(*)));
  output;
 end;
 drop test:;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 27 Aug 2019 21:53:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Arrays-lookup-and-Transpose/m-p/584377#M166445</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-08-27T21:53:20Z</dc:date>
    </item>
    <item>
      <title>Re: Arrays, lookup and Transpose</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Arrays-lookup-and-Transpose/m-p/584410#M166453</link>
      <description>&lt;P&gt;I really cannot figure out what you are asking.&lt;/P&gt;
&lt;P&gt;You original code is dynamic already.&amp;nbsp; Change the list of variables names in the macro variables and re-run to program to tranpose different variables.&lt;/P&gt;
&lt;P&gt;What is the purpose of the change?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What do you think that you are going to "lookup"?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do you just need help transferring data from a dataset into macro variables?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
  select source
       , sourcec
    into :mvars separated by ' '
       , :mvarsc separated by ' '
  from lookup
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 28 Aug 2019 03:27:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Arrays-lookup-and-Transpose/m-p/584410#M166453</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-08-28T03:27:39Z</dc:date>
    </item>
    <item>
      <title>Re: Arrays, lookup and Transpose</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Arrays-lookup-and-Transpose/m-p/584444#M166465</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp; Thanks! for the Hash enumerate and having all the variables including _C ones. You had answered another question I was researching on - creating an array from another one using first do loop.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp; That's how I am getting 2 lists of macros, however the second list is dependent on first one using lookup which wasn't established in my code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&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-left" image-alt="lkup.png" style="width: 208px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/32073iA7DA6739B670C958/image-size/medium?v=v2&amp;amp;px=400" role="button" title="lkup.png" alt="lkup.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="lia-img-tmp-id-1"&gt;Here's the sections: can we get mvarsc macro using source rather than reading as a independent column.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;array avars[*] $ &amp;amp;mvars. ;
   array avarsc[*] &amp;amp;mvarsc.;
      
  do i = 1 to dim(avars);
  	RES = avars[i];
	RESN = input(avarsc[i], ??best.);
  end; &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2) Another question while working on this&amp;nbsp;- we have avarsc[1] = Test1, assigning as RES=avarsc[1] gets the value of variable&amp;nbsp;Test1 to RES.&lt;/P&gt;&lt;P&gt;Now for RESN - using the format approach we can get RESN=put(avarsc[1], $srcf.) we get RESN=TEST1_C.&amp;nbsp; is there a way to assign here value of the variable Test1_C like its in PDV get that value of the variable.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Macros we do that using &amp;amp;&amp;amp; and &amp;amp;&amp;amp;&amp;amp;&amp;nbsp; to go one more level to get the value.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any material to read up on this will be great.&lt;/P&gt;</description>
      <pubDate>Wed, 28 Aug 2019 07:13:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Arrays-lookup-and-Transpose/m-p/584444#M166465</guid>
      <dc:creator>Rkvi</dc:creator>
      <dc:date>2019-08-28T07:13:09Z</dc:date>
    </item>
    <item>
      <title>Re: Arrays, lookup and Transpose</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Arrays-lookup-and-Transpose/m-p/584530#M166509</link>
      <description>&lt;P&gt;I still don't get it.&lt;/P&gt;
&lt;P&gt;If the second name is derived from the first name then change the column definition in the SELECT statement.&lt;/P&gt;
&lt;PRE&gt;select var,put(var,$fmt.) into ....&lt;/PRE&gt;
&lt;P&gt;If you want to find the value of a variable whose name you don't know until run time you will need to use VVALUEX() function.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;name='TEST';
value=vvaluex(name);
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note this will get you the formatted (character) value of the variable.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Aug 2019 13:58:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Arrays-lookup-and-Transpose/m-p/584530#M166509</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-08-28T13:58:32Z</dc:date>
    </item>
    <item>
      <title>Re: Arrays, lookup and Transpose</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Arrays-lookup-and-Transpose/m-p/584702#M166571</link>
      <description>&lt;P&gt;Thanks for sharing about VVALUEX.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Let me rephrase the problem: For have dataset with one TEST1 in a wide format:&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-left" image-alt="have.png" style="width: 409px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/32090i9711FAF55611A071/image-size/large?v=v2&amp;amp;px=999" role="button" title="have.png" alt="have.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&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-left" image-alt="lookup.png" style="width: 224px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/32092iFAA14015894E0B6E/image-size/large?v=v2&amp;amp;px=999" role="button" title="lookup.png" alt="lookup.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Transpose the data to have two new variables: RES - containing value of TEST1 variable.&lt;/P&gt;&lt;P&gt;RESN - using TEST1 variable, check in lookup and it has Code2: assign values of this as follows:&amp;nbsp;&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-left" image-alt="want1.png" style="width: 296px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/32093i75163BFC36706774/image-size/large?v=v2&amp;amp;px=999" role="button" title="want1.png" alt="want1.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For RESN: if the lookup updates to use Code1 for Test1, we get RESN values of Code1 variable 9, 4 as below:&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-left" image-alt="want2.png" style="width: 305px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/32094i51C0A3022A935CFC/image-size/large?v=v2&amp;amp;px=999" role="button" title="want2.png" alt="want2.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Once we add more columns to HAVE dataset in the wide format&amp;nbsp;along with&amp;nbsp;updated lookup, using the dynamic code we should get a normalized dataset.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Aug 2019 18:11:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Arrays-lookup-and-Transpose/m-p/584702#M166571</guid>
      <dc:creator>Rkvi</dc:creator>
      <dc:date>2019-08-28T18:11:50Z</dc:date>
    </item>
    <item>
      <title>Re: Arrays, lookup and Transpose</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Arrays-lookup-and-Transpose/m-p/584790#M166633</link>
      <description>&lt;P&gt;Which is what your original program was doing.&lt;/P&gt;
&lt;P&gt;The only issue with your ARRAY approach is that all of the variables in target list need to use the same type, but since you a pushing the results into the same variable then it shouldn't matter.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Another way is to just use your "lookup" table to generate some "wallpaper" code.&amp;nbsp; For example a step like this converts each observation into two output observations.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  res=test1;
  resn = code1;
  output;
  res=test2;
  resn = code2;
  output;
  keep subj res resn;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Here is logic to generate that type of code from a file that has the names of the variable pairs and then use %INCLUDE to run that code in a data step.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename code temp;
data _null_;
 set lookup;
 put 'res='  source ';'
   / 'resn=' source_c ';'
  / 'output;'
  ;
run;
data want;
  set have;
  %include code / source2;
  keep subj res resn;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Aug 2019 21:11:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Arrays-lookup-and-Transpose/m-p/584790#M166633</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-08-28T21:11:26Z</dc:date>
    </item>
  </channel>
</rss>

