<?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: Dataset to array in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Dataset-to-array/m-p/911715#M359501</link>
    <description>&lt;P&gt;Examples of providing your data in the form of data step code. Then using a data step merge, another way to match&amp;nbsp; up data on the value of one or more variables after sorting. Only tricky bit is the data set option to indicate which names come from DS2 to create a boolean (true/false 1/0) temporary variable that can be tested to filter on.&lt;/P&gt;
&lt;PRE&gt;data ds1;
  infile datalines dlm=',';
  input Names $ Descriptions :$25.;
datalines;
John,Tall guy
Eclipse,Red Car
Toby,Back and white dog
Eliazar,Refugee
Jose,Naturalized
;

 

data ds2;
   input ID $	Names $;
datalines;
5842 	Jose
6359 	Toby
8852 	Jaime
7821 	Eclipse
5636 	Brenda
;

proc sort data=ds1;
  by names;
run;
proc sort data=ds2;
  by names;
run;

data want;
  merge ds2 (in=inds2)
        ds1
  ;
  by names;
  if inds2;
run;&lt;/PRE&gt;
&lt;P&gt;Or Proc sql left join:&lt;/P&gt;
&lt;PRE&gt;proc sql;
   create table want2 as
   select ds2.id, ds2.names, ds1.descriptions
   from ds2
        left join
        ds1
        on ds2.names=ds1.names
   ;
quit;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 16 Jan 2024 23:15:20 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2024-01-16T23:15:20Z</dc:date>
    <item>
      <title>Dataset to array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dataset-to-array/m-p/911706#M359495</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a dataset with two variables; one is named "Names" and the other one is named "Descriptions".&amp;nbsp; I have 71 rows in this dataset.&amp;nbsp;&amp;nbsp; I have another dataset with multiple variables and one variable named "Names".&amp;nbsp;&amp;nbsp; I want to load dataset #1 into an array.&amp;nbsp; Then I want to match the variable "names" in dataset #2 to the array "Name", if it matches, then create a new variable with the content of the array "Description".&amp;nbsp; If it doesn't match, then leave the new variable empty.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Based on the put statement in the second data step, I am creating the arrays correctly (With the right content), but the problem comes i the last data step.&amp;nbsp; Any help?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;
Data _null_;
Set DS1 nobs=num_obs;
call symput('num_obs_array',num_obs);
stop;
run;
%Put  Observations= &amp;amp;num_obs_array.;

 
Data _null_;
Set DS1 end=eof;

if _N_=1 then do;
     array Name[&amp;amp;num_obs_array.] $32.;
     array Description[&amp;amp;num_obs_array.] $32000;
End;

Name[_N_]=Names;
Description[_N_]=Descriptions;

 if eof then do;
     do i = 1 to &amp;amp;num_obs_array.;
           put 'Name['i'] = ' name[i];
           put 'Description['i'] = ' Description[i];
     end;
end;

retain Name Description;
run;


Data DS_Want;
Set DS2;

if _N_=1 then do;
     array Name[&amp;amp;num_obs_array.] $32.;
     array Description[&amp;amp;num_obs_array.] $32000;
end;

 Length Description_New $32000.;

do i = 1 to &amp;amp;num_obs_array.;

     if strip(names)=strip(Name[i]) then do;
           Description_New=Description[i];
           leave;
     end;
end;
Run;&lt;/PRE&gt;</description>
      <pubDate>Tue, 16 Jan 2024 22:12:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dataset-to-array/m-p/911706#M359495</guid>
      <dc:creator>ismahero2</dc:creator>
      <dc:date>2024-01-16T22:12:47Z</dc:date>
    </item>
    <item>
      <title>Re: Dataset to array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dataset-to-array/m-p/911707#M359496</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/3008"&gt;@ismahero2&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a dataset with two variables; one is named "Names" and the other one is named "Descriptions".&amp;nbsp; I have 71 rows in this dataset.&amp;nbsp;&amp;nbsp; I have another dataset with multiple variables and one variable named "Names".&amp;nbsp;&amp;nbsp; I want to load dataset #1 into an array.&amp;nbsp; Then I want to match the variable "names" in dataset #2 to the array "Name", if it matches, then create a new variable with the content of the array "Description".&amp;nbsp; If it doesn't match, then leave the new variable empty.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Based on the put statement in the second data step, I am creating the arrays correctly (With the right content), but the problem comes i the last data step.&amp;nbsp; Any help?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;It would be extremely helpful if you could show us a small example of this problem, so we can see the datasets (or small examples of these datasets) that you are working with. Please follow these &lt;A href="https://blogs.sas.com/content/sastraining/2016/03/11/jedi-sas-tricks-data-to-data-step-macro/" target="_self"&gt;examples and instructions&lt;/A&gt; to provide us example SAS data sets we can work with.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It would be extremely helpful if you could show us the output you are getting, and then next show us the output you want to get. Saying that "the problem comes in the last data step" doesn't mean anything to anyone, you haven't even described what is wrong, and you haven't described the desired result.&lt;/P&gt;</description>
      <pubDate>Tue, 16 Jan 2024 22:26:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dataset-to-array/m-p/911707#M359496</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-01-16T22:26:28Z</dc:date>
    </item>
    <item>
      <title>Re: Dataset to array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dataset-to-array/m-p/911708#M359497</link>
      <description>&lt;P&gt;Why does this need to be an array. The usual (and efficient) two approaches for such lookups of values over a key are:&lt;/P&gt;
&lt;P&gt;Option 1: Create a format from the table with key/value pairs, add an other case that return a blank, use the format in the data step.&lt;/P&gt;
&lt;P&gt;Option 2: Load the table into a data step hash lookup table and lookup the values via the find() method.&lt;/P&gt;</description>
      <pubDate>Tue, 16 Jan 2024 22:36:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dataset-to-array/m-p/911708#M359497</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-01-16T22:36:29Z</dc:date>
    </item>
    <item>
      <title>Re: Dataset to array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dataset-to-array/m-p/911709#M359498</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is a sample of dataset #1 (DS1)&lt;/P&gt;
&lt;TABLE border="1" width="100%"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="lia-align-center"&gt;Names&lt;/TD&gt;
&lt;TD class="lia-align-center"&gt;Descriptions&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="50%"&gt;John&lt;/TD&gt;
&lt;TD width="50%"&gt;Tall guy&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="50%"&gt;Eclipse&lt;/TD&gt;
&lt;TD width="50%"&gt;Red Car&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="50%"&gt;Toby&lt;/TD&gt;
&lt;TD width="50%"&gt;Back and white dog&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="50%"&gt;Eliazar&lt;/TD&gt;
&lt;TD width="50%"&gt;Refugee &lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="50%"&gt;Jose&lt;/TD&gt;
&lt;TD width="50%"&gt;Naturalized &lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is an example of the second dataset (DS2)&lt;/P&gt;
&lt;TABLE border="1" width="100%"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="50%" class="lia-align-center"&gt;ID&lt;/TD&gt;
&lt;TD width="50%" class="lia-align-center"&gt;Names&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="50%"&gt;5842&lt;/TD&gt;
&lt;TD width="50%"&gt;Jose&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="50%"&gt;6359&lt;/TD&gt;
&lt;TD width="50%"&gt;Toby&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="50%"&gt;8852&lt;/TD&gt;
&lt;TD width="50%"&gt;Jaime&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="50%"&gt;7821&lt;/TD&gt;
&lt;TD width="50%"&gt;Eclipse&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="50%"&gt;5636&lt;/TD&gt;
&lt;TD width="50%"&gt;Brenda&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is what I would like to have:&lt;/P&gt;
&lt;TABLE border="1" width="75%"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="25%" class="lia-align-center"&gt;ID&lt;/TD&gt;
&lt;TD width="25%" class="lia-align-center"&gt;Names&lt;/TD&gt;
&lt;TD width="25%" class="lia-align-center"&gt;Descriptions&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="25%"&gt;5842&lt;/TD&gt;
&lt;TD width="25%"&gt;Jose&lt;/TD&gt;
&lt;TD width="25%"&gt;Naturalized&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="25%"&gt;6359&lt;/TD&gt;
&lt;TD width="25%"&gt;Toby&lt;/TD&gt;
&lt;TD width="25%"&gt;Back and white dog&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="25%"&gt;8852&lt;/TD&gt;
&lt;TD width="25%"&gt;Jaime&lt;/TD&gt;
&lt;TD width="25%"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="25%"&gt;7821&lt;/TD&gt;
&lt;TD width="25%"&gt;Eclipse&lt;/TD&gt;
&lt;TD width="25%"&gt;Red Car&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="25%"&gt;5636&lt;/TD&gt;
&lt;TD width="25%"&gt;Brenda&lt;/TD&gt;
&lt;TD width="25%"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The results I am receiving from the last data set have the third column empty.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you for your help.&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;</description>
      <pubDate>Tue, 16 Jan 2024 22:39:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dataset-to-array/m-p/911709#M359498</guid>
      <dc:creator>ismahero2</dc:creator>
      <dc:date>2024-01-16T22:39:39Z</dc:date>
    </item>
    <item>
      <title>Re: Dataset to array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dataset-to-array/m-p/911712#M359499</link>
      <description>Could you give me a sample code?  I have never used hash lookup. &lt;BR /&gt;</description>
      <pubDate>Tue, 16 Jan 2024 22:48:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dataset-to-array/m-p/911712#M359499</guid>
      <dc:creator>ismahero2</dc:creator>
      <dc:date>2024-01-16T22:48:25Z</dc:date>
    </item>
    <item>
      <title>Re: Dataset to array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dataset-to-array/m-p/911714#M359500</link>
      <description>&lt;P&gt;In the future if you're after actual code please provide sample data via tested SAS data step code the way I've done below for tables lookup and have. This allows us to spend the time answering your question instead of preparing sample data.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data lookup;
  infile datalines dsd truncover;
  input Names :$20. Descriptions :$40.;
  datalines;
John,Tall guy
Eclipse,Red Car
Toby,Back and white dog
Eliazar,Refugee
Jose,Naturalized
;

data have;
  infile datalines dsd truncover;
  input ID :$10. Names :$20.;
  datalines;
5842,Jose
6359,Toby
8852,Jaime
7821,Eclipse
5636,Brenda
;

data want;
  set have;
  if _n_=1 then
    do;
      if 0 then set lookup(keep=Descriptions);
      dcl hash h1(dataset:'lookup');
      h1.defineKey('Names');
      h1.defineData('Descriptions');
      h1.defineDone();
    end;
  if h1.find() ne 0 then call missing(Descriptions);
run;

proc print data=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Jan 2024 23:01:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dataset-to-array/m-p/911714#M359500</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-01-16T23:01:35Z</dc:date>
    </item>
    <item>
      <title>Re: Dataset to array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dataset-to-array/m-p/911715#M359501</link>
      <description>&lt;P&gt;Examples of providing your data in the form of data step code. Then using a data step merge, another way to match&amp;nbsp; up data on the value of one or more variables after sorting. Only tricky bit is the data set option to indicate which names come from DS2 to create a boolean (true/false 1/0) temporary variable that can be tested to filter on.&lt;/P&gt;
&lt;PRE&gt;data ds1;
  infile datalines dlm=',';
  input Names $ Descriptions :$25.;
datalines;
John,Tall guy
Eclipse,Red Car
Toby,Back and white dog
Eliazar,Refugee
Jose,Naturalized
;

 

data ds2;
   input ID $	Names $;
datalines;
5842 	Jose
6359 	Toby
8852 	Jaime
7821 	Eclipse
5636 	Brenda
;

proc sort data=ds1;
  by names;
run;
proc sort data=ds2;
  by names;
run;

data want;
  merge ds2 (in=inds2)
        ds1
  ;
  by names;
  if inds2;
run;&lt;/PRE&gt;
&lt;P&gt;Or Proc sql left join:&lt;/P&gt;
&lt;PRE&gt;proc sql;
   create table want2 as
   select ds2.id, ds2.names, ds1.descriptions
   from ds2
        left join
        ds1
        on ds2.names=ds1.names
   ;
quit;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Jan 2024 23:15:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dataset-to-array/m-p/911715#M359501</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-01-16T23:15:20Z</dc:date>
    </item>
    <item>
      <title>Re: Dataset to array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dataset-to-array/m-p/911716#M359502</link>
      <description>&lt;P&gt;And here sample code for creating a format. Creating a format has in your case the advantage that once created you can use it in multiple data steps or even directly in Proc's without even having to create a new variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data lookup;
  infile datalines dsd truncover;
  input Names :$20. Descriptions :$40.;
  datalines;
John,Tall guy
Eclipse,Red Car
Toby,Back and white dog
Eliazar,Refugee
Jose,Naturalized
;

data v_lookup /view=v_lookup;
  set lookup(keep=names descriptions rename=(names=start descriptions=label)) end=last;
  retain fmtname '$name_desc' type 'c';
  output;
  if last then
    do;
      hlo='O';
      label=' ';
      output;
    end;
run;

proc format cntlin=v_lookup;
run;

data have;
  infile datalines dsd truncover;
  input ID :$10. Names :$20.;
  datalines;
5842,Jose
6359,Toby
8852,Jaime
7821,Eclipse
5636,Brenda
;

data want;
  set have;
  descriptions=put(names,$name_desc.);
run;

proc print data=want;
run;

proc print data=have;
  format names $name_desc.;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 16 Jan 2024 23:16:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dataset-to-array/m-p/911716#M359502</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-01-16T23:16:34Z</dc:date>
    </item>
    <item>
      <title>Re: Dataset to array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dataset-to-array/m-p/911717#M359503</link>
      <description>&lt;P&gt;Thank you so much!&lt;/P&gt;</description>
      <pubDate>Tue, 16 Jan 2024 23:21:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dataset-to-array/m-p/911717#M359503</guid>
      <dc:creator>ismahero2</dc:creator>
      <dc:date>2024-01-16T23:21:45Z</dc:date>
    </item>
  </channel>
</rss>

