<?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: Wide to Long in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Wide-to-Long/m-p/767218#M243207</link>
    <description>&lt;P&gt;Thanks, &lt;BR /&gt;3 questions please:&lt;BR /&gt;1- Whay Varname=vname(vars[_i]) is not enought and you write the long code&lt;BR /&gt;varname =prxchange('s/^(.*)_\d{4}$/$1/oi',1,strip(vname(vars[_i])))&lt;BR /&gt;2- Why do you write scan with -1 and not +1 ? As I understand you take the date YYMM that is right to _ &lt;BR /&gt;3-Is the location of DROP can be also below do loop (After End )?&lt;BR /&gt;Which variables you delete with _: (I understand that you delete all vars with start of _ but can you write its name to understand )&lt;BR /&gt;thanks&lt;/P&gt;</description>
    <pubDate>Sat, 11 Sep 2021 18:00:47 GMT</pubDate>
    <dc:creator>Ronein</dc:creator>
    <dc:date>2021-09-11T18:00:47Z</dc:date>
    <item>
      <title>Wide to Long</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Wide-to-Long/m-p/767186#M243189</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;What are the ways to restructure the data from wide to long structure.&lt;/P&gt;
&lt;P&gt;Is it possible to use function VNAME here?&lt;/P&gt;
&lt;P&gt;IF we use proc transose then is it essential to perform 2 proc tranpose or we can do it in one step?&lt;/P&gt;
&lt;P&gt;thanks&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have;
Input PatientID Wage_2101 Wage 2104 Wage 2107 Staus_2101 Status_2104 Status_2107;
Cards;
1 100 150 80 200 2 2 2 1
2 200 180 300 250 1 1 1 1 
;
Run;

Data want;
Input PatientID YYMM Var_name value;
Cards;
1 Wage 100
1 Wage 150
1 Wage 80
1 Wage 200
1 Staus 2
1 Staus 2
1 Staus 2
1 Staus 1
2 Wage 200
2 Wage 180
2 Wage 300
2 Wage 250
2 Wage 1
2 Staus 1
2 Staus 1
2 Staus 1
;
Run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 11 Sep 2021 07:59:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Wide-to-Long/m-p/767186#M243189</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2021-09-11T07:59:24Z</dc:date>
    </item>
    <item>
      <title>Re: Wide to Long</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Wide-to-Long/m-p/767187#M243190</link>
      <description>&lt;P&gt;Use PROC TRANSPOSE first, and then do a data step to dissect _NAME_ into VAR_NAME and YYMM with the SCAN() function.&lt;/P&gt;</description>
      <pubDate>Sat, 11 Sep 2021 08:40:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Wide-to-Long/m-p/767187#M243190</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-09-11T08:40:04Z</dc:date>
    </item>
    <item>
      <title>Re: Wide to Long</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Wide-to-Long/m-p/767189#M243191</link>
      <description>&lt;P&gt;Below should work.&lt;/P&gt;
&lt;P&gt;Thanks for the data steps creating sample data ...but please next time try to post something that doesn't require fixing.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input PatientID Wage_2101 Wage_2104 Wage_2107 Staus_2101 Status_2104 Status_2107;
  datalines;
1 100 150 80 200 2 2 2 1
2 200 180 300 250 1 1 1 1 
;

proc sql noprint;
  select name into :varlist separated by ' '
  from dictionary.columns
  where 
    libname='WORK'
    and memname='HAVE'
    and upcase(name) ne 'PATIENTID'
  ;
quit;

data want;
  set have;
  array vars {*} &amp;amp;varlist;
  drop &amp;amp;varlist _:;
  length varname $32;

  do _i=1 to dim(vars);
    varname =prxchange('s/^(.*)_\d{4}$/$1/oi',1,strip(vname(vars[_i])));
    date    =input(scan(vname(vars[_i]),-1,'_'),yymmn4.);
    value   =vars[_i];
    format date date9.;
    output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 11 Sep 2021 08:52:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Wide-to-Long/m-p/767189#M243191</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2021-09-11T08:52:29Z</dc:date>
    </item>
    <item>
      <title>Re: Wide to Long</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Wide-to-Long/m-p/767200#M243199</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;'s approach is best. Here is code for how to do it.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=have out=have_t;
	by patientid;
run;

data want(rename=(col1=value));
	set have_t;
	yymm=scan(_name_,2,'_');
	var_name=scan(_name_,1,'_');
	
	drop _name_;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 11 Sep 2021 11:24:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Wide-to-Long/m-p/767200#M243199</guid>
      <dc:creator>tarheel13</dc:creator>
      <dc:date>2021-09-11T11:24:58Z</dc:date>
    </item>
    <item>
      <title>Re: Wide to Long</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Wide-to-Long/m-p/767218#M243207</link>
      <description>&lt;P&gt;Thanks, &lt;BR /&gt;3 questions please:&lt;BR /&gt;1- Whay Varname=vname(vars[_i]) is not enought and you write the long code&lt;BR /&gt;varname =prxchange('s/^(.*)_\d{4}$/$1/oi',1,strip(vname(vars[_i])))&lt;BR /&gt;2- Why do you write scan with -1 and not +1 ? As I understand you take the date YYMM that is right to _ &lt;BR /&gt;3-Is the location of DROP can be also below do loop (After End )?&lt;BR /&gt;Which variables you delete with _: (I understand that you delete all vars with start of _ but can you write its name to understand )&lt;BR /&gt;thanks&lt;/P&gt;</description>
      <pubDate>Sat, 11 Sep 2021 18:00:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Wide-to-Long/m-p/767218#M243207</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2021-09-11T18:00:47Z</dc:date>
    </item>
    <item>
      <title>Re: Wide to Long</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Wide-to-Long/m-p/767228#M243209</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks, &lt;BR /&gt;3 questions please:&lt;BR /&gt;1- Whay Varname=vname(vars[_i]) is not enought and you write the long code&lt;BR /&gt;varname =prxchange('s/^(.*)_\d{4}$/$1/oi',1,strip(vname(vars[_i])))&lt;BR /&gt;2- Why do you write scan with -1 and not +1 ? As I understand you take the date YYMM that is right to _ &lt;BR /&gt;3-Is the location of DROP can be also below do loop (After End )?&lt;BR /&gt;Which variables you delete with _: (I understand that you delete all vars with start of _ but can you write its name to understand )&lt;BR /&gt;thanks&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I think the regular expression is trying to take the name up any suffix of a underscore followed by four 4 digits.&amp;nbsp; &amp;nbsp;If the variable name does not have the suffix then VARNAME will be blank.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The negative index means to take the words counting from the end of the string instead of the beginning.&amp;nbsp; SCAN(x,-1,'_') means take the last word while scan(x,2,'_') means take the second word. If you have any names with two or more underscores the result will be different.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;DROP statement just instructs the compiler about what variables will be output to the dataset.&amp;nbsp; So they are not executable hence they can be anywhere in the data step.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Another way to get the suffix and remainder it so get the suffix first and then use SUBSTRN() to take the other part. That method will work even when there are more than one delimiter in the name.&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;name='A_B_C';
suffix=scan(name,-1,'_');
base=substrn(name,1,length(name)-length(suffix)-1);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 11 Sep 2021 19:25:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Wide-to-Long/m-p/767228#M243209</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-09-11T19:25:55Z</dc:date>
    </item>
  </channel>
</rss>

