<?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: How to program for missing values when transposing, without PROC TRANSPOSE or Array in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-program-for-missing-values-when-transposing-without-PROC/m-p/773399#M245692</link>
    <description>&lt;P&gt;Try my MERGE skill:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://support.sas.com/resources/papers/proceedings15/2785-2015.pdf" target="_blank"&gt;https://support.sas.com/resources/papers/proceedings15/2785-2015.pdf&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input patient visit v_date :date9. weight gender $;
format v_date yymmdd10.;
datalines;
1 1 11aug2006 158 Female
1 2 18sep2006 160 Female
;

proc sql;
select distinct catt('have(where=(visit=',visit,') 
 rename=(weight=weight_',visit,' v_date=v_date_',visit,'))') into : merge separated by ' '
 from have;
quit;
data want;
 merge &amp;amp;merge;
 by patient gender;
 drop visit;
run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 11 Oct 2021 13:00:03 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2021-10-11T13:00:03Z</dc:date>
    <item>
      <title>How to program for missing values when transposing, without PROC TRANSPOSE or Array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-program-for-missing-values-when-transposing-without-PROC/m-p/773278#M245627</link>
      <description>&lt;P&gt;Need to transpose an imported data set using only DATA step (if you wonder why it is required for class)&lt;/P&gt;&lt;P&gt;I have the original dataset with patients and visits&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Mruizv_0-1633889729180.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/64561iEAE89096B528730D/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Mruizv_0-1633889729180.png" alt="Mruizv_0-1633889729180.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;it is required to create a new set with the weights separated by visit, while using the retain option it does not program for missing values and maintains the value until a patient with more visits appears.&lt;/P&gt;&lt;P&gt;I have tried the null statement, missing statement, changing the visit value, changing weight per visit to missing if value is missing, and also setting weight value to missing&lt;/P&gt;&lt;P&gt;Current code looks something like this&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;DATA&lt;/STRONG&gt; fat (keep= patient gender group weight1 weight2 weight3 weight4);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; set icdb.clinical;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; by patient visit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if visit = '1' then weight1 = weight;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else if visit = '2' then weight2 = weight;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else if visit = '3' then weight3 = weight;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else if visit = '4' then weight4 = weight;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if last.patient then output;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; retain weight1 weight2 weight3 weight4;*keeping the new variables until next patient*;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;RUN&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 10 Oct 2021 18:18:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-program-for-missing-values-when-transposing-without-PROC/m-p/773278#M245627</guid>
      <dc:creator>Mruizv</dc:creator>
      <dc:date>2021-10-10T18:18:58Z</dc:date>
    </item>
    <item>
      <title>Re: How to program for missing values when transposing, without PROC TRANSPOSE or Array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-program-for-missing-values-when-transposing-without-PROC/m-p/773284#M245629</link>
      <description>&lt;P&gt;After you output, you must also set the 4 weight variables to missing.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 10 Oct 2021 20:35:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-program-for-missing-values-when-transposing-without-PROC/m-p/773284#M245629</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-10-10T20:35:46Z</dc:date>
    </item>
    <item>
      <title>Re: How to program for missing values when transposing, without PROC TRANSPOSE or Array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-program-for-missing-values-when-transposing-without-PROC/m-p/773288#M245630</link>
      <description>would that be right after the retain or inside the retain?&lt;BR /&gt;retain var1-var4 .;</description>
      <pubDate>Sun, 10 Oct 2021 21:44:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-program-for-missing-values-when-transposing-without-PROC/m-p/773288#M245630</guid>
      <dc:creator>Mruizv</dc:creator>
      <dc:date>2021-10-10T21:44:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to program for missing values when transposing, without PROC TRANSPOSE or Array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-program-for-missing-values-when-transposing-without-PROC/m-p/773303#M245638</link>
      <description>&lt;P&gt;Besides of the need to set all the retained variable to missing after the output statement...&lt;/P&gt;
&lt;PRE&gt;if last.patient then do; output; call missing(of weight1-weight4); end;&lt;/PRE&gt;
&lt;P&gt;...consider if you need to transpose your data at all.&lt;/P&gt;
&lt;P&gt;Keeping the data in a long and narrow structure is often better and a lot of SAS Proc's are very "happy" with by group processing and categorical variables for grouping (often defined via a CLASS statement in the Proc).&lt;/P&gt;</description>
      <pubDate>Mon, 11 Oct 2021 01:19:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-program-for-missing-values-when-transposing-without-PROC/m-p/773303#M245638</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2021-10-11T01:19:37Z</dc:date>
    </item>
    <item>
      <title>Re: How to program for missing values when transposing, without PROC TRANSPOSE or Array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-program-for-missing-values-when-transposing-without-PROC/m-p/773312#M245641</link>
      <description>&lt;P&gt;Understand this is requested for class.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Show what the expected result actually should look like for that example as I am not understanding.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I used to get into trouble with teachers by asking "why" when assigned to do something I was pretty sure was, let us say 'sub-optimal' at best, and would ask where the proposed changed structure is better.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the word "spreadsheet" or "Excel" is in the answer run screaming into the night.&lt;/P&gt;</description>
      <pubDate>Mon, 11 Oct 2021 04:38:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-program-for-missing-values-when-transposing-without-PROC/m-p/773312#M245641</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-10-11T04:38:03Z</dc:date>
    </item>
    <item>
      <title>Re: How to program for missing values when transposing, without PROC TRANSPOSE or Array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-program-for-missing-values-when-transposing-without-PROC/m-p/773316#M245643</link>
      <description>&lt;P&gt;I really hope that your teacher explains, that transposing perfectly formed data is something to be avoided.&lt;/P&gt;
&lt;P&gt;Have you worked with arrays? This is a perfect scenario to use them, because the variable "visit" can be used as index.&lt;/P&gt;
&lt;LI-SPOILER&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Array version */
data fat(keep= patient gender group weight1 weight2 weight3 weight4);
	set icdb.clinical;
	by patient; /* you have multiple obs per patient, not per visit */
	
	length weight1-weight4 8;
	retain weight1-weight4; 
	
	array weights [4] weight1-weight4;

	if first.patient then do;
		call missing(of weights); /* reset all weights */
	end;
	
	weights[visit] = weight;
	
	if last.patient then do;
		output;
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/LI-SPOILER&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 11 Oct 2021 06:06:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-program-for-missing-values-when-transposing-without-PROC/m-p/773316#M245643</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2021-10-11T06:06:38Z</dc:date>
    </item>
    <item>
      <title>Re: How to program for missing values when transposing, without PROC TRANSPOSE or Array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-program-for-missing-values-when-transposing-without-PROC/m-p/773325#M245648</link>
      <description>&lt;P&gt;Basically, your teacher wants you to recreate what PROC TRANSPOSE does in a long-to-wide transformation.&lt;/P&gt;
&lt;P&gt;This requires two steps:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;determining the items needed in the horizontal structure&lt;/LI&gt;
&lt;LI&gt;then set up that structure and populate it&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;In your case, we first need to determine the maximum number of visits present in the dataset:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
select max(visit) into :maxvisit trimmed from have;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then you use an array in the data step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
if 0 then set have; /* retrieves variable metadata */
array weights {&amp;amp;maxvisit.} weight1-weight&amp;amp;maxvisit.;
do until (last.patient);
  set have;
  by patient;
  weights{visit} = weight;
end;
keep patient gender weight1-weight&amp;amp;maxvisit.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Using a DO loop for each patient makes sure that the variables in the array are set to missing before each patient.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This code is untested; for tested code, please provide source data in a data step with datalines, so we can recreate your dataset with a simple copy/paste and submit. Do &lt;U&gt;not&lt;/U&gt; post pictures of data.&lt;/P&gt;
&lt;P&gt;Like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input patient visit v_date :date9. weight gender $;
format v_date yymmdd10.;
datalines;
1 1 11aug2006 158 Female
1 2 18sep2006 160 Female
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;My footnotes contain a link to a macro that can convert an existing dataset to such code automatically.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Use the "little running man" button next to the one indicated for posting SAS code:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Bildschirmfoto 2020-04-07 um 08.32.59.jpg" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/54552i914D97BE1B0F21E5/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Bildschirmfoto 2020-04-07 um 08.32.59.jpg" alt="Bildschirmfoto 2020-04-07 um 08.32.59.jpg" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;The indicated button is for logs and other text where the content and formatting must not be changed upon posting.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PS and if your teacher wants you to actually not even use an array, then a step that works without losing data (as you can't know the maximum number of visits beforehand in the real world) will need macro coding to dynamically create the assignment statements for the weightX variables:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro transpose;
data want;
if 0 then set have;
do until (last.patient);
  set have;
  by patient;
  select (visit);
  %do i = 1 %to &amp;amp;maxvisit.;
    when (&amp;amp;i) weight&amp;amp;i. = weight;
  %end;
  end;
end;
keep patient gender weight1-weight&amp;amp;maxvisit.;
run;
%mend;
%transpose&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This code is tested based on the made-up data.&lt;/P&gt;</description>
      <pubDate>Mon, 11 Oct 2021 08:31:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-program-for-missing-values-when-transposing-without-PROC/m-p/773325#M245648</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-10-11T08:31:41Z</dc:date>
    </item>
    <item>
      <title>Re: How to program for missing values when transposing, without PROC TRANSPOSE or Array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-program-for-missing-values-when-transposing-without-PROC/m-p/773349#M245661</link>
      <description>&lt;P&gt;this is what I end up doing to restart the retained value, homework are small exercises to test what was learned in class during the week. In this case it was output and retain functions, arrays are coming on the next unit so it will simplify doing this long process&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 11 Oct 2021 10:44:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-program-for-missing-values-when-transposing-without-PROC/m-p/773349#M245661</guid>
      <dc:creator>Mruizv</dc:creator>
      <dc:date>2021-10-11T10:44:24Z</dc:date>
    </item>
    <item>
      <title>Re: How to program for missing values when transposing, without PROC TRANSPOSE or Array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-program-for-missing-values-when-transposing-without-PROC/m-p/773350#M245662</link>
      <description>&lt;P&gt;I was going to make a comment about how this assignment wants the user to do things the hard way, and then achieve a data structure that is harder to work with than the original data structure. But I'm not going to say that.&lt;/P&gt;</description>
      <pubDate>Mon, 11 Oct 2021 10:48:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-program-for-missing-values-when-transposing-without-PROC/m-p/773350#M245662</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-10-11T10:48:30Z</dc:date>
    </item>
    <item>
      <title>Re: How to program for missing values when transposing, without PROC TRANSPOSE or Array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-program-for-missing-values-when-transposing-without-PROC/m-p/773351#M245663</link>
      <description>&lt;P&gt;Perfectly understandable that I would need to add the counter in the real world.&lt;/P&gt;&lt;P&gt;As it was just a little set I was able to see it before working on it, I have learnt so much about arrays with this community, this lesson was about OUTPUT and RETAIN so unfortunately the array is not an option until the next lesson. Also was required to be done within a single data step. The then-do did the trick for me&lt;/P&gt;</description>
      <pubDate>Mon, 11 Oct 2021 10:49:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-program-for-missing-values-when-transposing-without-PROC/m-p/773351#M245663</guid>
      <dc:creator>Mruizv</dc:creator>
      <dc:date>2021-10-11T10:49:36Z</dc:date>
    </item>
    <item>
      <title>Re: How to program for missing values when transposing, without PROC TRANSPOSE or Array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-program-for-missing-values-when-transposing-without-PROC/m-p/773378#M245682</link>
      <description>&lt;P&gt;You can easily un-macro the code to work with a fixed set:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
if 0 then set have;
do until (last.patient);
  set have;
  by patient;
  select (visit);
    when (1) weight1 = weight;
    when (2) weight2 = weight;
    when (3) weight3 = weight;
    when (4) weight4 = weight;
  end;
end;
keep patient gender weight1-weight4;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The nice thing about reading a group in a DO loop is that you do not have to worry when to reset the variables to missing, as the data step does it for you.&lt;/P&gt;</description>
      <pubDate>Mon, 11 Oct 2021 12:01:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-program-for-missing-values-when-transposing-without-PROC/m-p/773378#M245682</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-10-11T12:01:05Z</dc:date>
    </item>
    <item>
      <title>Re: How to program for missing values when transposing, without PROC TRANSPOSE or Array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-program-for-missing-values-when-transposing-without-PROC/m-p/773399#M245692</link>
      <description>&lt;P&gt;Try my MERGE skill:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://support.sas.com/resources/papers/proceedings15/2785-2015.pdf" target="_blank"&gt;https://support.sas.com/resources/papers/proceedings15/2785-2015.pdf&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input patient visit v_date :date9. weight gender $;
format v_date yymmdd10.;
datalines;
1 1 11aug2006 158 Female
1 2 18sep2006 160 Female
;

proc sql;
select distinct catt('have(where=(visit=',visit,') 
 rename=(weight=weight_',visit,' v_date=v_date_',visit,'))') into : merge separated by ' '
 from have;
quit;
data want;
 merge &amp;amp;merge;
 by patient gender;
 drop visit;
run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 11 Oct 2021 13:00:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-program-for-missing-values-when-transposing-without-PROC/m-p/773399#M245692</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-10-11T13:00:03Z</dc:date>
    </item>
  </channel>
</rss>

