<?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 Trouble with proc transpose / emulate pivot_longer from R in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Trouble-with-proc-transpose-emulate-pivot-longer-from-R/m-p/774306#M246084</link>
    <description>&lt;P&gt;I am working on the Tidy Tuesday data for this week about seafood.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Below is reproducible code. It works fine up until PROC TRANSPOSE, I am having trouble getting the syntax correct. Could anyone help me figure out the correct way to do this? I included a picture example from R at the end for what I am trying to do.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;* Get data 1;
filename test1234 url "https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2021/2021-10-12/seafood-and-fish-production-thousand-tonnes.csv";

proc import out=production datafile=test1234 dbms=csv replace; 
	guessingrows = max; 
	getnames=no;
run;

* Delete row 1 because getnames=no put the column names in the first row;
proc sql;
	delete from production
	where VAR1 = "Entity";
quit;

* Rename columns;
data production;
   set production (rename=(Var1=Entity
   						   Var2=Code
   						   Var3=Year
   						   Var4=Pelagic
   						   Var5=Crustaceans
   						   Var6=Cephalopods
   						   Var7=Demersal
   						   Var8=Freshwater
   						   Var9=Molluscs
   						   Var10=Other_Marine));
run;

* Delete all rows that have blank Code. Could not figure out how to filter it in the data step with where;
proc sql;
	delete from production
	where Code = "";
quit;

* Filter Entity to remove World and set year to max. Would prefer to do something like Year = max(Year);
data production;
	set production;
	where Entity ^= 'World' AND Year = '2013';
run;
&lt;BR /&gt;&lt;BR /&gt;
* ISSUES START BELOW HERE;
* Now the data is ready to pivot. Seems like proc transpose does this;
proc transpose data=production out=long_production;
	by Entity-Year;
	var Crustaceans-Other_Marine;
run;

* ERROR: CRUSTACEANS does not have a numeric suffix.;
proc transpose data=production out=long_production;
	by Year;
	var Crustaceans-Other_Marine;
run;

* ERROR: CRUSTACEANS does not have a numeric suffix.;
proc transpose data=production out=long_production;
	by Code;
	var Crustaceans-Other_Marine;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Example of what I am trying to do, but this is from R.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Indescribled_0-1634234796391.png" style="width: 547px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/64724i440AB8116AFC03FA/image-dimensions/547x312?v=v2" width="547" height="312" role="button" title="Indescribled_0-1634234796391.png" alt="Indescribled_0-1634234796391.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 14 Oct 2021 18:10:38 GMT</pubDate>
    <dc:creator>Indescribled</dc:creator>
    <dc:date>2021-10-14T18:10:38Z</dc:date>
    <item>
      <title>Trouble with proc transpose / emulate pivot_longer from R</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trouble-with-proc-transpose-emulate-pivot-longer-from-R/m-p/774306#M246084</link>
      <description>&lt;P&gt;I am working on the Tidy Tuesday data for this week about seafood.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Below is reproducible code. It works fine up until PROC TRANSPOSE, I am having trouble getting the syntax correct. Could anyone help me figure out the correct way to do this? I included a picture example from R at the end for what I am trying to do.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;* Get data 1;
filename test1234 url "https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2021/2021-10-12/seafood-and-fish-production-thousand-tonnes.csv";

proc import out=production datafile=test1234 dbms=csv replace; 
	guessingrows = max; 
	getnames=no;
run;

* Delete row 1 because getnames=no put the column names in the first row;
proc sql;
	delete from production
	where VAR1 = "Entity";
quit;

* Rename columns;
data production;
   set production (rename=(Var1=Entity
   						   Var2=Code
   						   Var3=Year
   						   Var4=Pelagic
   						   Var5=Crustaceans
   						   Var6=Cephalopods
   						   Var7=Demersal
   						   Var8=Freshwater
   						   Var9=Molluscs
   						   Var10=Other_Marine));
run;

* Delete all rows that have blank Code. Could not figure out how to filter it in the data step with where;
proc sql;
	delete from production
	where Code = "";
quit;

* Filter Entity to remove World and set year to max. Would prefer to do something like Year = max(Year);
data production;
	set production;
	where Entity ^= 'World' AND Year = '2013';
run;
&lt;BR /&gt;&lt;BR /&gt;
* ISSUES START BELOW HERE;
* Now the data is ready to pivot. Seems like proc transpose does this;
proc transpose data=production out=long_production;
	by Entity-Year;
	var Crustaceans-Other_Marine;
run;

* ERROR: CRUSTACEANS does not have a numeric suffix.;
proc transpose data=production out=long_production;
	by Year;
	var Crustaceans-Other_Marine;
run;

* ERROR: CRUSTACEANS does not have a numeric suffix.;
proc transpose data=production out=long_production;
	by Code;
	var Crustaceans-Other_Marine;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Example of what I am trying to do, but this is from R.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Indescribled_0-1634234796391.png" style="width: 547px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/64724i440AB8116AFC03FA/image-dimensions/547x312?v=v2" width="547" height="312" role="button" title="Indescribled_0-1634234796391.png" alt="Indescribled_0-1634234796391.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 14 Oct 2021 18:10:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trouble-with-proc-transpose-emulate-pivot-longer-from-R/m-p/774306#M246084</guid>
      <dc:creator>Indescribled</dc:creator>
      <dc:date>2021-10-14T18:10:38Z</dc:date>
    </item>
    <item>
      <title>Re: Trouble with proc transpose / emulate pivot_longer from R</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trouble-with-proc-transpose-emulate-pivot-longer-from-R/m-p/774318#M246093</link>
      <description>&lt;P&gt;There's one rule to follow to ensure these issues don't happen, in any language. Make sure your data is read in correctly.&lt;/P&gt;
&lt;P&gt;Spending a few minutes ensuring your types and names are correct at the beginning saves you a lot of headaches in the long run,.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This can be simplified further - by naming the variables in the import directly instead of in a second step and including some of the filters in that step.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could also likely read it directly into the last format from the CSV/TEXT file but I'll leave that as an exercise for you.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's my version of your code, some modifications:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;* Get data 1;
filename test1234 url "https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2021/2021-10-12/seafood-and-fish-production-thousand-tonnes.csv";

 data WORK.PRODUCTION    ;
             %let _EFIERR_ = 0; /* set the ERROR detection macro variable */
              infile TEST1234 delimiter = ',' MISSOVER DSD lrecl=32767 firstobs=2 ;
                 informat VAR1 $57. ;
                 informat VAR2 $8. ;
                 informat VAR3 $4. ;
                 informat VAR4-VAR10 8. ;
 
                 format VAR1 $57. ;
                 format VAR2 $8. ;
                 format VAR3 $4. ;
                 format VAR4-VAR10 8. ;

             input VAR1-VAR10;
                        
             ;
             if _ERROR_ then call symputx('_EFIERR_',1);  /* set ERROR detection macro variable */
             run;
 
 

* Rename columns;
data production2;
   set production (rename=(Var1=Entity
   						   Var2=Code
   						   Var3=Year
   						   Var4=Pelagic
   						   Var5=Crustaceans
   						   Var6=Cephalopods
   						   Var7=Demersal
   						   Var8=Freshwater
   						   Var9=Molluscs
   						   Var10=Other_Marine));
run;

*clean up and filter;
proc sql;
create table production3 as
select *
from production2 
where ENTITY not in ('Entity', 'World') and not missing(Code) 
having year=max(year);
quit;




* ISSUES START BELOW HERE;
* Now the data is ready to pivot. Seems like proc transpose does this;
proc transpose data=production3 out=long_production (rename = (_name_ = Seafood col1=Production));
	by Entity Year;
	var Crustaceans--Other_Marine;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/392714"&gt;@Indescribled&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I am working on the Tidy Tuesday data for this week about seafood.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Below is reproducible code. It works fine up until PROC TRANSPOSE, I am having trouble getting the syntax correct. Could anyone help me figure out the correct way to do this? I included a picture example from R at the end for what I am trying to do.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=""&gt;* Get data 1;
filename test1234 url "https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2021/2021-10-12/seafood-and-fish-production-thousand-tonnes.csv";

proc import out=production datafile=test1234 dbms=csv replace; 
	guessingrows = max; 
	getnames=no;
run;

* Delete row 1 because getnames=no put the column names in the first row;
proc sql;
	delete from production
	where VAR1 = "Entity";
quit;

* Rename columns;
data production;
   set production (rename=(Var1=Entity
   						   Var2=Code
   						   Var3=Year
   						   Var4=Pelagic
   						   Var5=Crustaceans
   						   Var6=Cephalopods
   						   Var7=Demersal
   						   Var8=Freshwater
   						   Var9=Molluscs
   						   Var10=Other_Marine));
run;

* Delete all rows that have blank Code. Could not figure out how to filter it in the data step with where;
proc sql;
	delete from production
	where Code = "";
quit;

* Filter Entity to remove World and set year to max. Would prefer to do something like Year = max(Year);
data production;
	set production;
	where Entity ^= 'World' AND Year = '2013';
run;
&lt;BR /&gt;&lt;BR /&gt;
* ISSUES START BELOW HERE;
* Now the data is ready to pivot. Seems like proc transpose does this;
proc transpose data=production out=long_production;
	by Entity-Year;
	var Crustaceans-Other_Marine;
run;

* ERROR: CRUSTACEANS does not have a numeric suffix.;
proc transpose data=production out=long_production;
	by Year;
	var Crustaceans-Other_Marine;
run;

* ERROR: CRUSTACEANS does not have a numeric suffix.;
proc transpose data=production out=long_production;
	by Code;
	var Crustaceans-Other_Marine;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Example of what I am trying to do, but this is from R.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Indescribled_0-1634234796391.png" style="width: 547px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/64724i440AB8116AFC03FA/image-dimensions/547x312?v=v2" width="547" height="312" role="button" title="Indescribled_0-1634234796391.png" alt="Indescribled_0-1634234796391.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 14 Oct 2021 18:55:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trouble-with-proc-transpose-emulate-pivot-longer-from-R/m-p/774318#M246093</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-10-14T18:55:34Z</dc:date>
    </item>
    <item>
      <title>Re: Trouble with proc transpose / emulate pivot_longer from R</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trouble-with-proc-transpose-emulate-pivot-longer-from-R/m-p/774320#M246094</link>
      <description>One big issue - you use a single hyphen when you need double hyphen. &lt;BR /&gt;&lt;BR /&gt;Crustaceans-Other_Marine would reference Crustaceans1-???A single hyphen relies on the index. If you're relying on positions of the columns then you need to use a double hyphen instead: Crustaceans -- Other_Marine;</description>
      <pubDate>Thu, 14 Oct 2021 18:58:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trouble-with-proc-transpose-emulate-pivot-longer-from-R/m-p/774320#M246094</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-10-14T18:58:30Z</dc:date>
    </item>
    <item>
      <title>Re: Trouble with proc transpose / emulate pivot_longer from R</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trouble-with-proc-transpose-emulate-pivot-longer-from-R/m-p/774347#M246106</link>
      <description>&lt;P&gt;Am I the only one curious about why random numerals in the R output have underlined (partial) values?&lt;/P&gt;</description>
      <pubDate>Thu, 14 Oct 2021 20:07:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trouble-with-proc-transpose-emulate-pivot-longer-from-R/m-p/774347#M246106</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-10-14T20:07:59Z</dc:date>
    </item>
    <item>
      <title>Re: Trouble with proc transpose / emulate pivot_longer from R</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trouble-with-proc-transpose-emulate-pivot-longer-from-R/m-p/774377#M246115</link>
      <description>That is a default feature of the Tidyverse R package. The underlining is to help identify big numbers I think, it ends where a comma would.</description>
      <pubDate>Thu, 14 Oct 2021 22:05:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trouble-with-proc-transpose-emulate-pivot-longer-from-R/m-p/774377#M246115</guid>
      <dc:creator>Indescribled</dc:creator>
      <dc:date>2021-10-14T22:05:39Z</dc:date>
    </item>
    <item>
      <title>Re: Trouble with proc transpose / emulate pivot_longer from R</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trouble-with-proc-transpose-emulate-pivot-longer-from-R/m-p/774380#M246117</link>
      <description>&lt;P&gt;Don't use PROC IMPORT to read a TEXT file, especially if you already know what is in the file.&lt;/P&gt;
&lt;P&gt;I wouldn't worry about eliminating entities or years (SAS is not R, you don't have to put all your data into memory).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data wide ;
  infile test1234 dsd truncover firstobs=2 ;
  input entity :$40. code :$8. year
        Pelagic Crustaceans Cephalopods Demersal Freshwater Molluscs Other_Marine
  ;
run;
proc transpose data=wide name=seafood out=want(rename=(col1=production));
  by entity code year;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 14 Oct 2021 22:13:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trouble-with-proc-transpose-emulate-pivot-longer-from-R/m-p/774380#M246117</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-10-14T22:13:43Z</dc:date>
    </item>
  </channel>
</rss>

