<?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: ERROR: Variable has been defined as both character and numeric. in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/ERROR-Variable-has-been-defined-as-both-character-and-numeric/m-p/686530#M24440</link>
    <description>&lt;P&gt;When you import from Excel you do not have control over the type of the data. You either need to convert it after the fact or you need to save the XLSX files as CSV and read them in and specify the correct types that way.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To convert a number to a character use PUT - but note that you need to change the variable name.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The format here should represent what you want the data to look like. You may also want to align it with the optional alignment parameter (-l).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Num2Char = put(varNum, 8. -l);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;To convert a character to a number use INPUT().&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Char2Num = input(varChar, 8.);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The informat here should represent what the data currently looks like.&amp;nbsp;&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/347487"&gt;@mcmaxwell&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi, I'm new to SAS Studio and I appreciate any help!&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have two excel data files that I am trying to merge. The first contains survey data from students in a course (45 variables and 491 observations). The second one contains exam scores from all students within the same course (3 variables, 619 observations).&lt;/P&gt;
&lt;P&gt;I am trying to one-to-one left merge the data using student ID number as the identifier, so that my new dataset contains all the information from the survey file and the exam scores only for the students who took my survey.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I import the files using the following SAS code:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/** Import survey file.  **/  
PROC IMPORT DATAFILE="/folders/myfolders/sasuser.v94/SMI PARTIALLY Coded Data.xlsx"  
		    OUT=L_DATA  
		    DBMS=XLSX  
		    REPLACE;  
RUN;  
  
/** Import grades file.  **/  
PROC IMPORT DATAFILE="/folders/myfolders/sasuser.v94/Exam 1 Scores.xlsx"  
		    OUT=R_DATA  
		    DBMS=XLSX  
		    REPLACE;  
RUN; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then, I create new ID columns that are characters rather than numbers:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;/** Create a new ID column as a character **/  
DATA LEFT; /* Names the new data LEFT */  
   set L_DATA (rename=(SUID=ID)); /* Says to use L_DATA data, and to rename variable SUID as ID */   
   SUID= put(ID,f5. -L); /* Not sure what this does. */  
   drop SUID; /* Drops the variable SUID */  
run; 

/** Create a new SUID column as Character in GRADES **/   
DATA RIGHT;    
   set R_DATA (rename=(SUID=ID));    
   SUID= put(ID,f5. -L);    
   drop SUID;   
run;  &lt;/PRE&gt;
&lt;P&gt;Then, I sort the datasets by ID&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/** Sort datasets by ID **/  
PROC SORT DATA = LEFT;  
	BY ID;   
RUN;  
PROC SORT DATA = RIGHT;  
	BY ID;   
RUN;  &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And finally, I try to merge the data sets:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/** Merge datasets **/ 
DATA ALL; 
	MERGE LEFT (IN=LEFT) RIGHT (IN=RIGHT); 
	BY ID; 
	IF LEFT;  
RUN; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Everything seems to work until this last step. I get the error message "ERROR: Variable ID has been defined as both character and numeric."&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How do I fix this? Is there a better way to import my data so I don't have to define the variables as characters each time?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks!&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;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 24 Sep 2020 21:09:41 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2020-09-24T21:09:41Z</dc:date>
    <item>
      <title>ERROR: Variable has been defined as both character and numeric.</title>
      <link>https://communities.sas.com/t5/New-SAS-User/ERROR-Variable-has-been-defined-as-both-character-and-numeric/m-p/686528#M24439</link>
      <description>&lt;P&gt;Hi, I'm new to SAS Studio and I appreciate any help!&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have two excel data files that I am trying to merge. The first contains survey data from students in a course (45 variables and 491 observations). The second one contains exam scores from all students within the same course (3 variables, 619 observations).&lt;/P&gt;&lt;P&gt;I am trying to one-to-one left merge the data using student ID number as the identifier, so that my new dataset contains all the information from the survey file and the exam scores only for the students who took my survey.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I import the files using the following SAS code:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/** Import survey file.  **/  
PROC IMPORT DATAFILE="/folders/myfolders/sasuser.v94/SMI PARTIALLY Coded Data.xlsx"  
		    OUT=L_DATA  
		    DBMS=XLSX  
		    REPLACE;  
RUN;  
  
/** Import grades file.  **/  
PROC IMPORT DATAFILE="/folders/myfolders/sasuser.v94/Exam 1 Scores.xlsx"  
		    OUT=R_DATA  
		    DBMS=XLSX  
		    REPLACE;  
RUN; &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Then, I create new ID columns that are characters rather than numbers:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;/** Create a new ID column as a character **/  
DATA LEFT; /* Names the new data LEFT */  
   set L_DATA (rename=(SUID=ID)); /* Says to use L_DATA data, and to rename variable SUID as ID */   
   SUID= put(ID,f5. -L); /* Not sure what this does. */  
   drop SUID; /* Drops the variable SUID */  
run; 

/** Create a new SUID column as Character in GRADES **/   
DATA RIGHT;    
   set R_DATA (rename=(SUID=ID));    
   SUID= put(ID,f5. -L);    
   drop SUID;   
run;  &lt;/PRE&gt;&lt;P&gt;Then, I sort the datasets by ID&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/** Sort datasets by ID **/  
PROC SORT DATA = LEFT;  
	BY ID;   
RUN;  
PROC SORT DATA = RIGHT;  
	BY ID;   
RUN;  &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;And finally, I try to merge the data sets:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/** Merge datasets **/ 
DATA ALL; 
	MERGE LEFT (IN=LEFT) RIGHT (IN=RIGHT); 
	BY ID; 
	IF LEFT;  
RUN; &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Everything seems to work until this last step. I get the error message "ERROR: Variable ID has been defined as both character and numeric."&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How do I fix this? Is there a better way to import my data so I don't have to define the variables as characters each time?&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&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>Thu, 24 Sep 2020 21:04:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/ERROR-Variable-has-been-defined-as-both-character-and-numeric/m-p/686528#M24439</guid>
      <dc:creator>mcmaxwell</dc:creator>
      <dc:date>2020-09-24T21:04:11Z</dc:date>
    </item>
    <item>
      <title>Re: ERROR: Variable has been defined as both character and numeric.</title>
      <link>https://communities.sas.com/t5/New-SAS-User/ERROR-Variable-has-been-defined-as-both-character-and-numeric/m-p/686530#M24440</link>
      <description>&lt;P&gt;When you import from Excel you do not have control over the type of the data. You either need to convert it after the fact or you need to save the XLSX files as CSV and read them in and specify the correct types that way.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To convert a number to a character use PUT - but note that you need to change the variable name.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The format here should represent what you want the data to look like. You may also want to align it with the optional alignment parameter (-l).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Num2Char = put(varNum, 8. -l);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;To convert a character to a number use INPUT().&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Char2Num = input(varChar, 8.);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The informat here should represent what the data currently looks like.&amp;nbsp;&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/347487"&gt;@mcmaxwell&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi, I'm new to SAS Studio and I appreciate any help!&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have two excel data files that I am trying to merge. The first contains survey data from students in a course (45 variables and 491 observations). The second one contains exam scores from all students within the same course (3 variables, 619 observations).&lt;/P&gt;
&lt;P&gt;I am trying to one-to-one left merge the data using student ID number as the identifier, so that my new dataset contains all the information from the survey file and the exam scores only for the students who took my survey.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I import the files using the following SAS code:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/** Import survey file.  **/  
PROC IMPORT DATAFILE="/folders/myfolders/sasuser.v94/SMI PARTIALLY Coded Data.xlsx"  
		    OUT=L_DATA  
		    DBMS=XLSX  
		    REPLACE;  
RUN;  
  
/** Import grades file.  **/  
PROC IMPORT DATAFILE="/folders/myfolders/sasuser.v94/Exam 1 Scores.xlsx"  
		    OUT=R_DATA  
		    DBMS=XLSX  
		    REPLACE;  
RUN; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then, I create new ID columns that are characters rather than numbers:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;/** Create a new ID column as a character **/  
DATA LEFT; /* Names the new data LEFT */  
   set L_DATA (rename=(SUID=ID)); /* Says to use L_DATA data, and to rename variable SUID as ID */   
   SUID= put(ID,f5. -L); /* Not sure what this does. */  
   drop SUID; /* Drops the variable SUID */  
run; 

/** Create a new SUID column as Character in GRADES **/   
DATA RIGHT;    
   set R_DATA (rename=(SUID=ID));    
   SUID= put(ID,f5. -L);    
   drop SUID;   
run;  &lt;/PRE&gt;
&lt;P&gt;Then, I sort the datasets by ID&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/** Sort datasets by ID **/  
PROC SORT DATA = LEFT;  
	BY ID;   
RUN;  
PROC SORT DATA = RIGHT;  
	BY ID;   
RUN;  &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And finally, I try to merge the data sets:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/** Merge datasets **/ 
DATA ALL; 
	MERGE LEFT (IN=LEFT) RIGHT (IN=RIGHT); 
	BY ID; 
	IF LEFT;  
RUN; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Everything seems to work until this last step. I get the error message "ERROR: Variable ID has been defined as both character and numeric."&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How do I fix this? Is there a better way to import my data so I don't have to define the variables as characters each time?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks!&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;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 24 Sep 2020 21:09:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/ERROR-Variable-has-been-defined-as-both-character-and-numeric/m-p/686530#M24440</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-09-24T21:09:41Z</dc:date>
    </item>
    <item>
      <title>Re: ERROR: Variable has been defined as both character and numeric.</title>
      <link>https://communities.sas.com/t5/New-SAS-User/ERROR-Variable-has-been-defined-as-both-character-and-numeric/m-p/686532#M24441</link>
      <description>&lt;P&gt;With the use of Excel files (the&amp;nbsp;&lt;STRONG&gt;WORST&lt;/STRONG&gt; file format for data transfer), you have to live with the guesses that SAS has to make about variable types.&lt;/P&gt;
&lt;P&gt;Save your data to text (e.g. csv) files, which you can read with data steps where&amp;nbsp;&lt;EM&gt;you&lt;/EM&gt; have control.&lt;/P&gt;
&lt;P&gt;Otherwise, you have to clean up the mess cause by the *#@%!? Excel files.&lt;/P&gt;
&lt;P&gt;A step like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA RIGHT;    
   set R_DATA (rename=(SUID=ID));    
   SUID= put(ID,f5. -L);    
   drop SUID;   
run;  &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;does not really convert anything, as you immediately throw away the newly created character variable.&lt;/P&gt;</description>
      <pubDate>Thu, 24 Sep 2020 21:15:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/ERROR-Variable-has-been-defined-as-both-character-and-numeric/m-p/686532#M24441</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-09-24T21:15:55Z</dc:date>
    </item>
  </channel>
</rss>

