<?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: Creating a new variable for each found entry in hash object in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable-for-each-found-entry-in-hash-object/m-p/722737#M224150</link>
    <description>&lt;P&gt;Please double check that first data step and post one that runs.&lt;/P&gt;
&lt;P&gt;Data for CARDS cannot be on the same line and I really suspect that you used something other than the SAS editor to make that and the result is something that is mashing the value for the first patient into the id of the second patient. Assuming that is the case, a join and transpose may be appropriate&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;Data patients;
   Input PatientID StartDate;
cards;
1 50
2 70
3 20
4 10
run; 

Data Treatments;
 Input PatientID TreatmentDate Score;
 format TreatmentDate date9.;
 cards;
1 20 1
1 40 3
1 60 4
2 30 1
4 20 1
4 30 2
4 40 3
4 50 4
4 60 7
4 70 10
run;
        

proc sql;
   create table temp as
   select a.*,b.score
   from patients as a 
        left join
        treatments as b
        on a.PatientID=b.patientid
   ;
quit;

proc transpose data=temp
   out=want (drop=_name_) prefix=Number
;
   by patientid startdate;
   var score;
run;
&lt;/PRE&gt;
&lt;P&gt;You don't mention what is supposed to happen with treatment dates but for your very limited example this works.&lt;/P&gt;
&lt;P&gt;If you expect the score values in a specific order that may or may not be possible depending on the rule. An order by clause in the Proc SQL may work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 01 Mar 2021 23:07:56 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2021-03-01T23:07:56Z</dc:date>
    <item>
      <title>Creating a new variable for each found entry in hash object</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable-for-each-found-entry-in-hash-object/m-p/722730#M224146</link>
      <description>&lt;P&gt;Hi there!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a table of patients (one line per patient) for whom i want to find a score in another table of treatments (several lines per patient, one score for each date). In my real search it is several thousands, but here it's boiled down to a simple example.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Patient-table:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data patients;
Input PatientID StartDate;
cards;
1 50
2 70
3 20
4 10
run; &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;Treatments-table:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data Treatments;
 Input PatientID TreatmentDate Score;
 format TreatmentDate date9.;
 cards;
1 20 1
1 40 3
1 60 4
2 30 1
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Then my goal is then to search for the treatments by using a hash-object. For each treatment-score that fulfills the criteria, i want to populate/create a new variable:&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;SPAN style="font-family: inherit;"&gt;Look-up code:&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data lookup;
	if _n_ = 1 then do;

	if 0 then set Treatments;
		dcl hash treat (dataset: 'Treatments', multidata: 'y');
		treat.definekey ('PatientID');
		treat.definedata ('TreatmentDate','Score');
		treat.definedone ();
	end;
	
	set patients;
	by patientid;
	if first.patientid then count = .;

	do _iorc_ = treat.find() by 0 while (_iorc_ = 0);
			if TreatmentDate &amp;lt; Startdate then do;
			Count + 1;
			call symput('counter',count);
			Number&amp;amp;Counter = Score;
			end;

		_iorc_ = treat.find_next();
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;SPAN style="font-family: inherit;"&gt;So that i end up with the final table looking like:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: inherit;"&gt;PatientID | StartDate | Number1 | Number2 | Number3 ... Number X&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: inherit;"&gt;1 | 50 | 1 | 3 | .&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: inherit;"&gt;2 | 70 | . | . | .&amp;nbsp;&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;Thanks for your time and help &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&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, 02 Mar 2021 08:28:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable-for-each-found-entry-in-hash-object/m-p/722730#M224146</guid>
      <dc:creator>Malthe</dc:creator>
      <dc:date>2021-03-02T08:28:15Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a new variable for each found entry in hash object</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable-for-each-found-entry-in-hash-object/m-p/722737#M224150</link>
      <description>&lt;P&gt;Please double check that first data step and post one that runs.&lt;/P&gt;
&lt;P&gt;Data for CARDS cannot be on the same line and I really suspect that you used something other than the SAS editor to make that and the result is something that is mashing the value for the first patient into the id of the second patient. Assuming that is the case, a join and transpose may be appropriate&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;Data patients;
   Input PatientID StartDate;
cards;
1 50
2 70
3 20
4 10
run; 

Data Treatments;
 Input PatientID TreatmentDate Score;
 format TreatmentDate date9.;
 cards;
1 20 1
1 40 3
1 60 4
2 30 1
4 20 1
4 30 2
4 40 3
4 50 4
4 60 7
4 70 10
run;
        

proc sql;
   create table temp as
   select a.*,b.score
   from patients as a 
        left join
        treatments as b
        on a.PatientID=b.patientid
   ;
quit;

proc transpose data=temp
   out=want (drop=_name_) prefix=Number
;
   by patientid startdate;
   var score;
run;
&lt;/PRE&gt;
&lt;P&gt;You don't mention what is supposed to happen with treatment dates but for your very limited example this works.&lt;/P&gt;
&lt;P&gt;If you expect the score values in a specific order that may or may not be possible depending on the rule. An order by clause in the Proc SQL may work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 01 Mar 2021 23:07:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable-for-each-found-entry-in-hash-object/m-p/722737#M224150</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-03-01T23:07:56Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a new variable for each found entry in hash object</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable-for-each-found-entry-in-hash-object/m-p/722798#M224178</link>
      <description>&lt;P&gt;&lt;EM&gt;&amp;gt; For each treatment-score that fulfills the criteria, i want to populate/create a new variable&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;You can't create variables on the fly. All variables are created when the data step compiles.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;To fill these variables:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;1. Create an array at the start of the data step&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;	array Number [99] ;
&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;2. Fill the array by changing your code from&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;	Number&amp;amp;Counter = Score;
&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;to&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;	Number[Count] = Score;
&lt;/LI-CODE&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, 02 Mar 2021 07:27:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable-for-each-found-entry-in-hash-object/m-p/722798#M224178</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-03-02T07:27:55Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a new variable for each found entry in hash object</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable-for-each-found-entry-in-hash-object/m-p/722814#M224186</link>
      <description>Thanks for your fast reply!&lt;BR /&gt;&lt;BR /&gt;1) That's weird. I took the code directly form SAS Enterprise Guide, something must have happened with the formatting that didn't show, when i was editing the post. It's fixed now!&lt;BR /&gt;&lt;BR /&gt;2) The treatment-dates are used to ensure, that only the scores from before the startdate (from patients), are outputted. It's in this step of the hash-do-loop:&lt;BR /&gt;&lt;BR /&gt;"if TreatmentDate &amp;lt; Startdate then do;"&lt;BR /&gt;&lt;BR /&gt;i can definitely use your method, but i think the array one in the other reply is easier to apply in this case &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Tue, 02 Mar 2021 08:48:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable-for-each-found-entry-in-hash-object/m-p/722814#M224186</guid>
      <dc:creator>Malthe</dc:creator>
      <dc:date>2021-03-02T08:48:09Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a new variable for each found entry in hash object</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable-for-each-found-entry-in-hash-object/m-p/722815#M224187</link>
      <description>Thanks a lot! Worked like a charm &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;</description>
      <pubDate>Tue, 02 Mar 2021 08:48:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable-for-each-found-entry-in-hash-object/m-p/722815#M224187</guid>
      <dc:creator>Malthe</dc:creator>
      <dc:date>2021-03-02T08:48:40Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a new variable for each found entry in hash object</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable-for-each-found-entry-in-hash-object/m-p/722832#M224199</link>
      <description>&lt;P&gt;Set your Enterprise Guide to not use tabs in the code. Replace tabs on loading files or while you type with blanks. Different systems use different tab widths.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Next, in your second data step you assign a date format to values that are quite clearly not SAS date values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Third, the TRANSPOSE method is to be preferred, because it automatically results in the minimal number of variables needed, completely data-driven. The array method must either use an array size with a substantial safety margin, resulting in lots of unneeded columns, wasting disk and screen space, or you run the risk of an array index overflowing, or dropping values (losing information) &lt;STRONG&gt;without noticing it&lt;/STRONG&gt;.&lt;/P&gt;</description>
      <pubDate>Tue, 02 Mar 2021 10:46:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable-for-each-found-entry-in-hash-object/m-p/722832#M224199</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-03-02T10:46:37Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a new variable for each found entry in hash object</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable-for-each-found-entry-in-hash-object/m-p/722835#M224200</link>
      <description>1) Noted! Thanks for mentioning that &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;BR /&gt;&lt;BR /&gt;2) In my real data sets those values are true dates. For this example it didn't matter, since it was only about getting the coding right - with some numbers representing dates. I could have left out the formatting.&lt;BR /&gt;&lt;BR /&gt;3) Ah, yeah, i see the underlying problem. Thanks &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;</description>
      <pubDate>Tue, 02 Mar 2021 11:04:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable-for-each-found-entry-in-hash-object/m-p/722835#M224200</guid>
      <dc:creator>Malthe</dc:creator>
      <dc:date>2021-03-02T11:04:44Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a new variable for each found entry in hash object</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable-for-each-found-entry-in-hash-object/m-p/722840#M224203</link>
      <description>&lt;P&gt;And another important hint for handling the code windows here:&lt;/P&gt;
&lt;P&gt;do not edit code by simply clicking on it and typing away; position the cursor in the window, hit the "little running man" (or &amp;lt;/&amp;gt;) button, and &lt;EM&gt;then&lt;/EM&gt; do your editing in the pop-up. When typing directly without calling up the pop-up, linefeeds do not work.&lt;/P&gt;</description>
      <pubDate>Tue, 02 Mar 2021 11:32:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable-for-each-found-entry-in-hash-object/m-p/722840#M224203</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-03-02T11:32:21Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a new variable for each found entry in hash object</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable-for-each-found-entry-in-hash-object/m-p/723017#M224293</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;Totally agree about never using tabs.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;About transposing, the downside is that you might end up with tables having different structures. Pre-defining the columns ensures that the table structure is consistent, and if the array happened to be undersized, SAS would certainly let you know with:&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier" color="#FF0000"&gt;ERROR: Array subscript out of range at line 29 column 26.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 02 Mar 2021 22:58:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable-for-each-found-entry-in-hash-object/m-p/723017#M224293</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-03-02T22:58:51Z</dc:date>
    </item>
  </channel>
</rss>

