<?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 Transposing a long dataset to wide with multiple variable types in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Transposing-a-long-dataset-to-wide-with-multiple-variable-types/m-p/987749#M380068</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 several variable types (char, num, date, time, checkboxes, string) and I am trying to convert it to a wide format with 1 row per ID. I have created RESP_C, RESP_N,&amp;nbsp;RESP_DATE and RESP_TIME variables to assign the correct the appropriate values to each of these columns.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data prepared;&lt;BR /&gt;set joined;length RESP_C $1000 RESP_N 8 RESP_DATE 12 RESP_TIME 8;&lt;/P&gt;&lt;P&gt;RESP_C = strip(Response);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;select (upcase(Target_Type));&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;when ('DATE') do;&lt;BR /&gt;RESP_DATE = input(strip(Response), anydtdte.);&lt;BR /&gt;format RESP_DATE date9.;&lt;BR /&gt;RESP_C = "";&lt;BR /&gt;end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;when ('TIME') do;&lt;BR /&gt;RESP_TIME = input(strip(Response), anydttme.); /* NA 4/29: timepart is for mixed reponse, so removed it (date + time) */&lt;BR /&gt;format RESP_TIME timeampm.;&lt;BR /&gt;RESP_C = "";&lt;BR /&gt;end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;when ('CHECK') do;&lt;BR /&gt;if strip(Response) in ('Y','YES','TRUE','1','CHECKED','Selected') then RESP_N = 1; /* NA 4/29: Added 'Selected' to the list as that is the response of the form */&lt;BR /&gt;else if missing(Response) then RESP_N = .;&lt;BR /&gt;else RESP_N = 0;&lt;BR /&gt;/*format RESP_N 1.; */&lt;BR /&gt;RESP_C = "";&lt;BR /&gt;end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;when ('NUM') do;&lt;BR /&gt;RESP_N = input(strip(Response), best32.);&lt;BR /&gt;RESP_C = "";&lt;BR /&gt;end;&lt;/P&gt;&lt;P&gt;otherwise do; /* CHAR */&lt;BR /&gt;/* keep RESP_C */&lt;BR /&gt;end;&lt;/P&gt;&lt;P&gt;end;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sort data=prepared;&lt;BR /&gt;by SID FormDateTime FirstName MiddleName LastName DOB;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/***********************************************************************&lt;BR /&gt;* 7) Transpose into wide:&lt;BR /&gt;* - Character targets (CHAR) -&amp;gt; wide_char&lt;BR /&gt;* - Numeric targets (DATE, TIME, NUM, CHECK) -&amp;gt; wide_num&lt;BR /&gt;***********************************************************************/&lt;/P&gt;&lt;P&gt;proc transpose data=prepared(where=(Target_Type='CHAR'))&lt;BR /&gt;out=wide_char(drop=_name_);&lt;BR /&gt;by SID FormDateTime FirstName MiddleName LastName DOB;&lt;BR /&gt;id VarName_ID;&lt;BR /&gt;var RESP_C;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;proc transpose data=prepared(where=(Target_Type in ('DATE','TIME','NUM','CHECK')))&lt;BR /&gt;out=wide_num(drop=_name_);&lt;BR /&gt;format RESP_DATE date9. RESP_TIME timeampm.;&lt;BR /&gt;by SID FormDateTime FirstName MiddleName LastName DOB;&lt;BR /&gt;id VarName_ID;&lt;BR /&gt;var RESP_N RESP_DATE RESP_TIME;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, this is only working on some date values in the dataset and not applying uniformly to all the values. Additionally, after transposing each variable type is a unique row and not one row per ID.&lt;/P&gt;&lt;P&gt;I would appreciate any help in fixing this code. Thank you!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 11 May 2026 19:27:42 GMT</pubDate>
    <dc:creator>NandiniA</dc:creator>
    <dc:date>2026-05-11T19:27:42Z</dc:date>
    <item>
      <title>Transposing a long dataset to wide with multiple variable types</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transposing-a-long-dataset-to-wide-with-multiple-variable-types/m-p/987749#M380068</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 several variable types (char, num, date, time, checkboxes, string) and I am trying to convert it to a wide format with 1 row per ID. I have created RESP_C, RESP_N,&amp;nbsp;RESP_DATE and RESP_TIME variables to assign the correct the appropriate values to each of these columns.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data prepared;&lt;BR /&gt;set joined;length RESP_C $1000 RESP_N 8 RESP_DATE 12 RESP_TIME 8;&lt;/P&gt;&lt;P&gt;RESP_C = strip(Response);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;select (upcase(Target_Type));&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;when ('DATE') do;&lt;BR /&gt;RESP_DATE = input(strip(Response), anydtdte.);&lt;BR /&gt;format RESP_DATE date9.;&lt;BR /&gt;RESP_C = "";&lt;BR /&gt;end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;when ('TIME') do;&lt;BR /&gt;RESP_TIME = input(strip(Response), anydttme.); /* NA 4/29: timepart is for mixed reponse, so removed it (date + time) */&lt;BR /&gt;format RESP_TIME timeampm.;&lt;BR /&gt;RESP_C = "";&lt;BR /&gt;end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;when ('CHECK') do;&lt;BR /&gt;if strip(Response) in ('Y','YES','TRUE','1','CHECKED','Selected') then RESP_N = 1; /* NA 4/29: Added 'Selected' to the list as that is the response of the form */&lt;BR /&gt;else if missing(Response) then RESP_N = .;&lt;BR /&gt;else RESP_N = 0;&lt;BR /&gt;/*format RESP_N 1.; */&lt;BR /&gt;RESP_C = "";&lt;BR /&gt;end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;when ('NUM') do;&lt;BR /&gt;RESP_N = input(strip(Response), best32.);&lt;BR /&gt;RESP_C = "";&lt;BR /&gt;end;&lt;/P&gt;&lt;P&gt;otherwise do; /* CHAR */&lt;BR /&gt;/* keep RESP_C */&lt;BR /&gt;end;&lt;/P&gt;&lt;P&gt;end;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sort data=prepared;&lt;BR /&gt;by SID FormDateTime FirstName MiddleName LastName DOB;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/***********************************************************************&lt;BR /&gt;* 7) Transpose into wide:&lt;BR /&gt;* - Character targets (CHAR) -&amp;gt; wide_char&lt;BR /&gt;* - Numeric targets (DATE, TIME, NUM, CHECK) -&amp;gt; wide_num&lt;BR /&gt;***********************************************************************/&lt;/P&gt;&lt;P&gt;proc transpose data=prepared(where=(Target_Type='CHAR'))&lt;BR /&gt;out=wide_char(drop=_name_);&lt;BR /&gt;by SID FormDateTime FirstName MiddleName LastName DOB;&lt;BR /&gt;id VarName_ID;&lt;BR /&gt;var RESP_C;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;proc transpose data=prepared(where=(Target_Type in ('DATE','TIME','NUM','CHECK')))&lt;BR /&gt;out=wide_num(drop=_name_);&lt;BR /&gt;format RESP_DATE date9. RESP_TIME timeampm.;&lt;BR /&gt;by SID FormDateTime FirstName MiddleName LastName DOB;&lt;BR /&gt;id VarName_ID;&lt;BR /&gt;var RESP_N RESP_DATE RESP_TIME;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, this is only working on some date values in the dataset and not applying uniformly to all the values. Additionally, after transposing each variable type is a unique row and not one row per ID.&lt;/P&gt;&lt;P&gt;I would appreciate any help in fixing this code. Thank you!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 11 May 2026 19:27:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transposing-a-long-dataset-to-wide-with-multiple-variable-types/m-p/987749#M380068</guid>
      <dc:creator>NandiniA</dc:creator>
      <dc:date>2026-05-11T19:27:42Z</dc:date>
    </item>
    <item>
      <title>Re: Transposing a long dataset to wide with multiple variable types</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transposing-a-long-dataset-to-wide-with-multiple-variable-types/m-p/987768#M380071</link>
      <description>&lt;P&gt;It looks like you currently have the data stored in a single character variable named RESPONSE.&lt;/P&gt;
&lt;P&gt;You appear to also have a variableTARGET_TYPE that appears to indicates what TYPE of variable needs to be created to store this particular RESPONSE.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What variable has the NAME that you want to use for the new variable?&lt;/P&gt;
&lt;P&gt;What are the variables that uniquely identify the set of observations you want to transpose into one?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do you need code that works for this particular dataset, with its specific set of variables?&lt;/P&gt;
&lt;P&gt;If so then just write some code that that reads in all of the observations for the output observation in one iteration of the data step.&lt;/P&gt;
&lt;P&gt;So something like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  do until (last.id);
    set have;
    by id;
    if NAME='DATE' then date=input(left(response),anydtdte.);
    else if NAME='AGE' then age=input(left(response),32.);
    ...
  end;
  drop NAME TARGET_TYPE RESPONSE ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or do you want a program that could convert ANY dataset?&amp;nbsp; If so then select the unique combinations of NAME and TARGET_TYPE from the dataset and use that to generate a program like the one above.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For a more detailed answer provide a small example input dataset (as a working DATA step).&amp;nbsp; Also provide a data step that creates the dataset you want to get from that example input.&lt;/P&gt;</description>
      <pubDate>Tue, 12 May 2026 00:45:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transposing-a-long-dataset-to-wide-with-multiple-variable-types/m-p/987768#M380071</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2026-05-12T00:45:41Z</dc:date>
    </item>
    <item>
      <title>Re: Transposing a long dataset to wide with multiple variable types</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transposing-a-long-dataset-to-wide-with-multiple-variable-types/m-p/987769#M380072</link>
      <description>&lt;P&gt;Since you are using this BY statement in your PROC TRANPOSE&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;by SID FormDateTime FirstName MiddleName LastName DOB;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;To get duplicate observations you would need to have changes in one of those variables.&amp;nbsp; Are you sure that FORMDATETIME is the same of all observations you want to combine? What about the other 5 varaibles?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or are you just talking about the fact that you generated TWO datasets, but did not include a step to combine them?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  merge wide_num wide_char;
  by SID FormDateTime FirstName MiddleName LastName DOB;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Also you will need to apply the FORMAT to the variables AFTEr the PROC TRANSPOSE step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example both DATE and TIME variables are numeric, so I would assume you put them into the WIDE_NUM dataset.&amp;nbsp; But if you do not attach a FORMAT to the resulting variable(s) then the values will be hard for humans to interpret.&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, 12 May 2026 01:28:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transposing-a-long-dataset-to-wide-with-multiple-variable-types/m-p/987769#M380072</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2026-05-12T01:28:34Z</dc:date>
    </item>
  </channel>
</rss>

