<?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: Sql in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Sql/m-p/789255#M252530</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;as in the previous answer, I think that there is no general solution.&lt;/P&gt;&lt;P&gt;However, there is a solution, if you know the categories of your transpose-id-variable (in you case "Name"). Since I don't know your data set, I will use the class dat set shipped with SAS:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data Test;
	set sashelp.class;
run;
proc sort data=Test; by Name Sex; run;
proc transpose data=Test out=Test_1;
	id Sex;
	by Name;
	var Height;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The Variable Sex has 2 categories 'F' and 'M'. If those are &lt;STRONG&gt;not changing&lt;/STRONG&gt;, you can write:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;proc sql;
	create table Test_2 as
		select Name,
			max( case when Sex = 'M' then Height else . end ) as M,
			max( case when Sex = 'F' then Height else . end ) as F
		from Test
		group by Name
		order by Name;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;However, if those categories are changing, you will need to program a SAS-Makro to retrieve the categories into a list and do a loop:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;%macro transCustom(intab, outtab, idvar, byvar, varvar);
	%local i numid listid;

	/* Get categories and store in a list. */
	proc sql noprint;
		select distinct &amp;amp;idvar.
		into :listid separated by ' '
		from &amp;amp;intab.
		order by &amp;amp;idvar.;
	quit;

	/* Number of elements in list. */
	%let numid=%sysfunc(countw(&amp;amp;listid.));

	proc sql;
		create table &amp;amp;outtab. as
			select &amp;amp;byvar.
				%do i=1 %to &amp;amp;numid.;
					%let curcat=%scan(&amp;amp;listid., &amp;amp;i.); /* Get i-th element from list. */
					, max( case when &amp;amp;idvar. = "&amp;amp;curcat." then &amp;amp;varvar. else . end ) as &amp;amp;curcat.
				%end;
			from &amp;amp;intab.
			group by &amp;amp;byvar.
			order by &amp;amp;byvar.;
	quit;
%mend;
%transCustom(Test, Test_3, Sex, Name, Height);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I hope this helps,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Daniel.&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>Mon, 10 Jan 2022 14:23:21 GMT</pubDate>
    <dc:creator>Daniel_Enache</dc:creator>
    <dc:date>2022-01-10T14:23:21Z</dc:date>
    <item>
      <title>Sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sql/m-p/788786#M252295</link>
      <description>Can I change those below sas codes into SQL?&lt;BR /&gt;&lt;BR /&gt;Proc sort data=dsn; by id;&lt;BR /&gt;&lt;BR /&gt;Proc transpose data=dsn; out=dsn_1;&lt;BR /&gt;By id;&lt;BR /&gt;Id name;&lt;BR /&gt;Var var1;&lt;BR /&gt;Run;&lt;BR /&gt;&lt;BR /&gt;Thank you.</description>
      <pubDate>Fri, 07 Jan 2022 03:47:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sql/m-p/788786#M252295</guid>
      <dc:creator>Emma2021</dc:creator>
      <dc:date>2022-01-07T03:47:30Z</dc:date>
    </item>
    <item>
      <title>Re: Sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sql/m-p/788789#M252298</link>
      <description>&lt;P&gt;Transposes are hard to do in SQL. In SAS SQL you would have to hard code the new variable names for example. Some SQL DB do support transpose using PIVOT or other similar functionality but none as nicely as PROC TRANSPOSE. &lt;BR /&gt;&lt;BR /&gt;So technically you can, but the exact code depends on the data and if the data changes the code has to change.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/381519"&gt;@Emma2021&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Can I change those below sas codes into SQL?&lt;BR /&gt;&lt;BR /&gt;Proc sort data=dsn; by id;&lt;BR /&gt;&lt;BR /&gt;Proc transpose data=dsn; out=dsn_1;&lt;BR /&gt;By id;&lt;BR /&gt;Id name;&lt;BR /&gt;Var var1;&lt;BR /&gt;Run;&lt;BR /&gt;&lt;BR /&gt;Thank you.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 07 Jan 2022 03:57:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sql/m-p/788789#M252298</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-01-07T03:57:18Z</dc:date>
    </item>
    <item>
      <title>Re: Sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sql/m-p/789255#M252530</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;as in the previous answer, I think that there is no general solution.&lt;/P&gt;&lt;P&gt;However, there is a solution, if you know the categories of your transpose-id-variable (in you case "Name"). Since I don't know your data set, I will use the class dat set shipped with SAS:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data Test;
	set sashelp.class;
run;
proc sort data=Test; by Name Sex; run;
proc transpose data=Test out=Test_1;
	id Sex;
	by Name;
	var Height;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The Variable Sex has 2 categories 'F' and 'M'. If those are &lt;STRONG&gt;not changing&lt;/STRONG&gt;, you can write:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;proc sql;
	create table Test_2 as
		select Name,
			max( case when Sex = 'M' then Height else . end ) as M,
			max( case when Sex = 'F' then Height else . end ) as F
		from Test
		group by Name
		order by Name;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;However, if those categories are changing, you will need to program a SAS-Makro to retrieve the categories into a list and do a loop:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;%macro transCustom(intab, outtab, idvar, byvar, varvar);
	%local i numid listid;

	/* Get categories and store in a list. */
	proc sql noprint;
		select distinct &amp;amp;idvar.
		into :listid separated by ' '
		from &amp;amp;intab.
		order by &amp;amp;idvar.;
	quit;

	/* Number of elements in list. */
	%let numid=%sysfunc(countw(&amp;amp;listid.));

	proc sql;
		create table &amp;amp;outtab. as
			select &amp;amp;byvar.
				%do i=1 %to &amp;amp;numid.;
					%let curcat=%scan(&amp;amp;listid., &amp;amp;i.); /* Get i-th element from list. */
					, max( case when &amp;amp;idvar. = "&amp;amp;curcat." then &amp;amp;varvar. else . end ) as &amp;amp;curcat.
				%end;
			from &amp;amp;intab.
			group by &amp;amp;byvar.
			order by &amp;amp;byvar.;
	quit;
%mend;
%transCustom(Test, Test_3, Sex, Name, Height);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I hope this helps,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Daniel.&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>Mon, 10 Jan 2022 14:23:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sql/m-p/789255#M252530</guid>
      <dc:creator>Daniel_Enache</dc:creator>
      <dc:date>2022-01-10T14:23:21Z</dc:date>
    </item>
  </channel>
</rss>

