<?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: Is there any quick way for below logic? transpose variables into columns with their values. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Is-there-any-quick-way-for-below-logic-transpose-variables-into/m-p/929761#M365810</link>
    <description>&lt;P&gt;You could use array processing.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want(keep=obs id dt varname res);
  set have;
  array test_vars {*} var:;
  length varname $32 res $2;
  do _i=1 to dim(test_vars);
    if test_vars[_i] ne ' ' then
      do;
        varname=vname(test_vars[_i]);
        res=test_vars[_i];
        output;
      end;
  end;
run;

proc print data=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1716779343076.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/96784i25F2E5AE779C278A/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1716779343076.png" alt="Patrick_0-1716779343076.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 27 May 2024 03:12:07 GMT</pubDate>
    <dc:creator>Patrick</dc:creator>
    <dc:date>2024-05-27T03:12:07Z</dc:date>
    <item>
      <title>Is there any quick way for below logic? transpose variables into columns with their values.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Is-there-any-quick-way-for-below-logic-transpose-variables-into/m-p/929758#M365808</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have data like below and I would like to convert variables as test with their result. I have created macro and calling a macro for every variable each time. But the problem is I have more than 4,00,000 records with 100 variables which means 100 tests, so it is taking lot of time for each test and when I am stacking them then I am getting low space pop up notification, and I am unable to run my program completely. Is there any solution for this?&amp;nbsp;&lt;/P&gt;&lt;P&gt;data I have:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data have;
input Obs $1-2 id $3-7 DT $8-17 var6_d0 $18-20 var20 $21-23  var30 $24-26 var40 $27-29 var70 $30-32 var6_d1 $33-35 var21 $36-38 var31 $39-41 var41 $42-44 var71 $45-47;
datalines;
1 1001 23MAY2023 No                           
2 1001 23MAY2023           No                 
3 1001 23MAY2023                             
4 1001 23MAY2023                             
1 1001 23MAY2023                             
2 1001 23MAY2023                             
3 1001 23MAY2023                             
4 1001 23MAY2023                             
1 1001 23MAY2023                             
2 1001 23MAY2023                             
3 1001 23MAY2023                             
4 1001 23MAY2023                             
1 1001 23MAY2023                             
2 1001 23MAY2023                             
3 1001 23MAY2023                             
4 1001 23MAY2023                             
1 1001 23MAY2023                             
2 1001 23MAY2023                             
3 1001 23MAY2023                             
4 1001 23MAY2023                             
1 1001 20JUN2023 No                           
2 1001 20JUN2023           No                 
3 1001 20JUN2023                             
4 1001 20JUN2023                             
1 1001 20JUN2023                             
2 1001 20JUN2023                             
3 1001 20JUN2023                             
4 1001 20JUN2023                             
1 1001 20JUN2023                             
2 1001 20JUN2023                             
3 1001 20JUN2023                             
4 1001 20JUN2023                             
1 1001 20JUN2023                             
2 1001 20JUN2023                             
3 1001 20JUN2023                             
4 1001 20JUN2023                             
1 1001 20JUN2023                             
2 1001 20JUN2023                             
3 1001 20JUN2023                             
4 1001 20JUN2023                             
1 1001 15AUG2023 No                           
2 1001 15AUG2023           No                 
3 1001 15AUG2023                             
4 1001 15AUG2023                             
1 1001 15AUG2023                             
2 1001 15AUG2023                             
3 1001 15AUG2023                             
4 1001 15AUG2023                             
1 1001 15AUG2023                             
2 1001 15AUG2023                             
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Macro I have created:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;&lt;PRE&gt;&lt;CODE class=""&gt;%macro convert_variables_columns(test=);
data want;
	set have;
	where &amp;amp;test. ne ""; 
	test=&amp;amp;test.;
	res=&amp;amp;test.;
run;
proc sort; by id; run;
%mend;

%convert_variables_columns(test=var6_d0);
%convert_variables_columns(test=var20);
%convert_variables_columns(test=var30);
%convert_variables_columns(test=var40);
%convert_variables_columns(test=var70);
%convert_variables_columns(test=var6_d1);
%convert_variables_columns(test=var21);
%convert_variables_columns(test=var31);
%convert_variables_columns(test=var41);
%convert_variables_columns(test=var71);&lt;/CODE&gt;&lt;/PRE&gt;Data I want:&lt;/DIV&gt;&lt;DIV&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data want;
input obs $1-2 id $3-7 dt $8-17 test $18-25 res $26-28;
datalines;
1 1001 23MAY2023 var6_d0 No
2 1001 23MAY2023 var40   No
1 1001 20JUN2023 var6_d0 No
2 1001 20JUN2023 var40   No
1 1001 15AUG2023 var6_d0 No
2 1001 15AUG2023 var40   No
;
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;/DIV&gt;&lt;DIV&gt;Thanks,&lt;/DIV&gt;&lt;DIV&gt;Chi&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 27 May 2024 02:43:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Is-there-any-quick-way-for-below-logic-transpose-variables-into/m-p/929758#M365808</guid>
      <dc:creator>chinna0369</dc:creator>
      <dc:date>2024-05-27T02:43:25Z</dc:date>
    </item>
    <item>
      <title>Re: Is there any quick way for below logic? transpose variables into columns with their values.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Is-there-any-quick-way-for-below-logic-transpose-variables-into/m-p/929759#M365809</link>
      <description>&lt;P&gt;Yes.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But it is very hard to follow what you want.&amp;nbsp; It would be much easier with a simple dataset that only has 3 or 4 variables and 5 to 10 observations.&amp;nbsp; Then you could explain exactly how you created the output you wanted from that simple input.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Does your dataset have any combination of variables that form a unique key?&amp;nbsp; Perhaps OBS, ID an DT ?&lt;/P&gt;
&lt;P&gt;If so it looks like you just want a single PROC TRANSPOSE step.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=have 
  out=want(rename=(_name_=test col1=res)
           where=(res ne ' ')  )
;
  by obs id dt notsorted;
  var var6_d0 -- var71;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;Obs    Obs     id            DT     test      res

 1      1     1001    23MAY2023    var6_d0    No
 2      2     1001    23MAY2023    var40      No
 3      1     1001    20JUN2023    var6_d0    No
 4      2     1001    20JUN2023    var40      No
 5      1     1001    15AUG2023    var6_d0    No
 6      2     1001    15AUG2023    var40      No
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 27 May 2024 03:00:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Is-there-any-quick-way-for-below-logic-transpose-variables-into/m-p/929759#M365809</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-05-27T03:00:16Z</dc:date>
    </item>
    <item>
      <title>Re: Is there any quick way for below logic? transpose variables into columns with their values.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Is-there-any-quick-way-for-below-logic-transpose-variables-into/m-p/929761#M365810</link>
      <description>&lt;P&gt;You could use array processing.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want(keep=obs id dt varname res);
  set have;
  array test_vars {*} var:;
  length varname $32 res $2;
  do _i=1 to dim(test_vars);
    if test_vars[_i] ne ' ' then
      do;
        varname=vname(test_vars[_i]);
        res=test_vars[_i];
        output;
      end;
  end;
run;

proc print data=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1716779343076.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/96784i25F2E5AE779C278A/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1716779343076.png" alt="Patrick_0-1716779343076.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 27 May 2024 03:12:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Is-there-any-quick-way-for-below-logic-transpose-variables-into/m-p/929761#M365810</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-05-27T03:12:07Z</dc:date>
    </item>
    <item>
      <title>Re: Is there any quick way for below logic? transpose variables into columns with their values.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Is-there-any-quick-way-for-below-logic-transpose-variables-into/m-p/929803#M365828</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/117414"&gt;@chinna0369&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have data like below and I would like to convert variables as test with their result. I have created macro and calling a macro for every variable each time. But the problem is I have more than 4,00,000 records with 100 variables which means 100 tests, so it is taking lot of time for each test and when I am stacking them then I am getting low space pop up notification, and I am unable to run my program completely.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hard to imagine this would take less space after you do the transpose. And when you do the transpose, it will take up twice the disk space, because now you have the original un-transposed data set and the transposed data set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What are the exact words of this low space notification?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What program are you trying to run when you get the low space notification? What does the program do? What analyses are you trying to perform when you run out of space?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Have you asked your system administrator for more disk space?&lt;/P&gt;</description>
      <pubDate>Mon, 27 May 2024 12:56:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Is-there-any-quick-way-for-below-logic-transpose-variables-into/m-p/929803#M365828</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-05-27T12:56:59Z</dc:date>
    </item>
  </channel>
</rss>

