<?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: turning wide data to long for many variables, extract suffix in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/turning-wide-data-to-long-for-many-variables-extract-suffix/m-p/792122#M253798</link>
    <description>&lt;P&gt;So I took it out of the do loop and ran each variable in the macro independently using ctrl+h&lt;/P&gt;</description>
    <pubDate>Tue, 25 Jan 2022 08:13:07 GMT</pubDate>
    <dc:creator>axescot78</dc:creator>
    <dc:date>2022-01-25T08:13:07Z</dc:date>
    <item>
      <title>turning wide data to long for many variables, extract suffix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/turning-wide-data-to-long-for-many-variables-extract-suffix/m-p/792076#M253791</link>
      <description>&lt;P&gt;I'm trying to turn wide data into long and all variables are suffixed with year. There are many variables (500+) so I am trying to write a macro to do this. The first 3 variables vary slightly (are not present for all years) so I did them individually, merge them, and then create an array of variable names which goes into a do loop to run the macro. The macro uses proc transpose and merges to the dataset that is already started. However, I am getting an error which I have narrowed down to the do loop. Any advice?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Data that I have:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input studyid group var1_01 var1_04 var1_07 var2_01 var2_04 var2_07;
datalines;
2218 3 1 0 1 1 1 0
3779 1 1 0 1 1 0 1
5914 1 1 0 1 1 0 1
;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Data want:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
input studyid group year var1 var2;
datalines;
2218 3 _01 1 1
2218 3 _04 0 1
2218 3 _07 1 0
3779 1 _01 1 1
3779 1 _04 0 0
3779 1 _07 1 1
5914 1 _01 1 1
5914 1 _04 0 0
5914 1 _07 1 1
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Macros:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%LET Y1 = '_01';
%LET Y2 = '_04';
%LET Y3 = '_07';

%MACRO reshape(col);
	proc transpose data=subset_cols 
			out=long_data 
			prefix=&amp;amp;col ;
		by studyid;
		var &amp;amp;col.&amp;amp;Y1 &amp;amp;col.&amp;amp;Y2 &amp;amp;col.&amp;amp;Y3 &amp;amp;co1.&amp;amp;Y4 &amp;amp;col.&amp;amp;Y5 ;

	data combined;
		merge combined 
			long_data (rename=(&amp;amp;col.1=&amp;amp;col)) ;
		by studyid;
		year = substr(_name_, length(_name_)-2, 3);
		drop _name_;

%MEND reshape;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do loop to call on macro:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data long_data_4;
	set wide_data;
	array col_names (*) 
		var4
		var5
		;

	do i=1 to dim(col_names);
		%reshape(col_names(i));
	end;

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Error that I am getting:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="log.PNG" style="width: 781px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/67718i26E9A4C4CFC36F4C/image-size/large?v=v2&amp;amp;px=999" role="button" title="log.PNG" alt="log.PNG" /&gt;&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;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 25 Jan 2022 07:53:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/turning-wide-data-to-long-for-many-variables-extract-suffix/m-p/792076#M253791</guid>
      <dc:creator>axescot78</dc:creator>
      <dc:date>2022-01-25T07:53:05Z</dc:date>
    </item>
    <item>
      <title>Re: turning wide data to long for many variables, extract suffix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/turning-wide-data-to-long-for-many-variables-extract-suffix/m-p/792085#M253792</link>
      <description>&lt;P&gt;Do all the variables begin with either var1 or var2?&lt;/P&gt;</description>
      <pubDate>Tue, 25 Jan 2022 07:35:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/turning-wide-data-to-long-for-many-variables-extract-suffix/m-p/792085#M253792</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-01-25T07:35:46Z</dc:date>
    </item>
    <item>
      <title>Re: turning wide data to long for many variables, extract suffix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/turning-wide-data-to-long-for-many-variables-extract-suffix/m-p/792100#M253793</link>
      <description>&lt;P&gt;No, they each have their own names. But I narrowed this down to the do loop. I am updating my post now.&lt;/P&gt;</description>
      <pubDate>Tue, 25 Jan 2022 07:46:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/turning-wide-data-to-long-for-many-variables-extract-suffix/m-p/792100#M253793</guid>
      <dc:creator>axescot78</dc:creator>
      <dc:date>2022-01-25T07:46:44Z</dc:date>
    </item>
    <item>
      <title>Re: turning wide data to long for many variables, extract suffix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/turning-wide-data-to-long-for-many-variables-extract-suffix/m-p/792102#M253794</link>
      <description>&lt;P&gt;See if you can use this as a template&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input studyid group var1_01 var1_04 var1_07 var2_01 var2_04 var2_07;
datalines;
2218 3 1 0 1 1 1 0
3779 1 1 0 1 1 0 1
5914 1 1 0 1 1 0 1
;

data want;
   set have;
   array _1 {*} var1:;
   array _2 {*} var2:;

   do i = 1 to dim(_1);
      year = scan(vname(_1[i]), -1, '_');
      var1 = _1[i];
      var2 = _2[i];
	  output;
   end;

   keep studyid group year var1 var2;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 25 Jan 2022 07:47:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/turning-wide-data-to-long-for-many-variables-extract-suffix/m-p/792102#M253794</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-01-25T07:47:27Z</dc:date>
    </item>
    <item>
      <title>Re: turning wide data to long for many variables, extract suffix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/turning-wide-data-to-long-for-many-variables-extract-suffix/m-p/792110#M253796</link>
      <description>&lt;P&gt;It's the way I am calling the macro because it works outside of the do loop&lt;/P&gt;</description>
      <pubDate>Tue, 25 Jan 2022 07:53:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/turning-wide-data-to-long-for-many-variables-extract-suffix/m-p/792110#M253796</guid>
      <dc:creator>axescot78</dc:creator>
      <dc:date>2022-01-25T07:53:39Z</dc:date>
    </item>
    <item>
      <title>Re: turning wide data to long for many variables, extract suffix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/turning-wide-data-to-long-for-many-variables-extract-suffix/m-p/792122#M253798</link>
      <description>&lt;P&gt;So I took it out of the do loop and ran each variable in the macro independently using ctrl+h&lt;/P&gt;</description>
      <pubDate>Tue, 25 Jan 2022 08:13:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/turning-wide-data-to-long-for-many-variables-extract-suffix/m-p/792122#M253798</guid>
      <dc:creator>axescot78</dc:creator>
      <dc:date>2022-01-25T08:13:07Z</dc:date>
    </item>
    <item>
      <title>Re: turning wide data to long for many variables, extract suffix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/turning-wide-data-to-long-for-many-variables-extract-suffix/m-p/792126#M253800</link>
      <description>&lt;P&gt;The error you get is because you are trying to run a macro that runs a PROC TRANSPOSE and a datastep inside a datastep DO loop. If you turn on option MPRINT, you will see what happens.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I assume that you want to run this type of transformation for many more variables. If they all have the same year suffixes, you can do something like this (based on the solution by &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;&amp;nbsp;)&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input studyid group var1_01 var1_04 var1_07 var2_01 var2_04 var2_07;
datalines;
2218 3 1 0 1 1 1 0
3779 1 1 0 1 1 0 1
5914 1 1 0 1 1 0 1
;run;

%macro transpose_vars(vars);             
  %local i w;                            
  %do i=1 %to %sysfunc(countw(&amp;amp;vars));   
    %let w=%scan(&amp;amp;vars,&amp;amp;i);              
    array _&amp;amp;i (*) &amp;amp;w:;                   
    %end;                                
  do _N_=1 to dim(_1);               
    length year $2;    
    year = scan(vname(_1(_N_)),-1,'_');  
    %do i=1 %to %sysfunc(countw(&amp;amp;vars)); 
      %let w=%scan(&amp;amp;vars,&amp;amp;i);            
      &amp;amp;w=_&amp;amp;i (_N_);                      
      %end;                              
    output;                              
    end;                                 
  keep studyid group year &amp;amp;vars;         
%mend;                                   
options mprint;                          
data want;                               
   set have;                             
   %transpose_vars(var1 var2);           
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 25 Jan 2022 08:40:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/turning-wide-data-to-long-for-many-variables-extract-suffix/m-p/792126#M253800</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2022-01-25T08:40:55Z</dc:date>
    </item>
    <item>
      <title>Re: turning wide data to long for many variables, extract suffix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/turning-wide-data-to-long-for-many-variables-extract-suffix/m-p/792127#M253801</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/76464"&gt;@s_lassen&lt;/a&gt;&amp;nbsp;cool &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 25 Jan 2022 08:43:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/turning-wide-data-to-long-for-many-variables-extract-suffix/m-p/792127#M253801</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-01-25T08:43:40Z</dc:date>
    </item>
    <item>
      <title>Re: turning wide data to long for many variables, extract suffix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/turning-wide-data-to-long-for-many-variables-extract-suffix/m-p/792138#M253804</link>
      <description>&lt;P&gt;Without any macro coding, using two TRANSPOSEs:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=have out=long1;
by studyid group;
var var:;
run;

data long2;
set long1;
year = scan(_name_,2,"_");
_name_ = scan(_name_,1,"_");
run;

proc sort data=long2;
by studyid group year;
run;

proc transpose data=long2 out=want (drop=_name_);
by studyid group year;
var col1;
id _name_;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The only thing you need to do is expanding the VAR statement in the first TRANSPOSE.&lt;/P&gt;</description>
      <pubDate>Tue, 25 Jan 2022 09:18:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/turning-wide-data-to-long-for-many-variables-extract-suffix/m-p/792138#M253804</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-01-25T09:18:41Z</dc:date>
    </item>
  </channel>
</rss>

