Help using Base SAS procedures

CHAR to NUMERIC

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 7
Accepted Solution

CHAR to NUMERIC

Hi,

i have to do a simply ( still not for me ) modify on an existing dataset.

  

I would change the format of a column from CHAR to NUMERIC.

 

Searching into SAS library, i seen the INPUT statement which i used to set a new column.  Actually I don't know how to modify an existant column.

 

 

Can someone help me ?

 

Thanks in advance,

regards,

D.


Accepted Solutions
Solution
‎12-15-2015 01:10 PM
Super User
Posts: 11,343

Re: CHAR to NUMERIC

Basically you cannot change variable type in an existing dataset. The general approach is to create a new data set as needed.

Generic code to do something like that is:

 

Data want;

   set have (rename= (charactervariablename= oldvar));

   input charactervariablename = input (oldvar,best32.); /* or if you have a specific informat planned use it*/

  drop oldvar;

run;

 

I would not recommend overwriting the existing have data set until verifying everything is correct. Then rename the Want to the existing set.

View solution in original post


All Replies
Solution
‎12-15-2015 01:10 PM
Super User
Posts: 11,343

Re: CHAR to NUMERIC

Basically you cannot change variable type in an existing dataset. The general approach is to create a new data set as needed.

Generic code to do something like that is:

 

Data want;

   set have (rename= (charactervariablename= oldvar));

   input charactervariablename = input (oldvar,best32.); /* or if you have a specific informat planned use it*/

  drop oldvar;

run;

 

I would not recommend overwriting the existing have data set until verifying everything is correct. Then rename the Want to the existing set.

Frequent Contributor
Posts: 108

Re: CHAR to NUMERIC

When changing types a new variable name is required. If you need to keep the original variable name, use the RENAME= option on
the SET statement to rename the variable as it comes into the PDV.
This allows the original variable name to be reused when you change type.
Respected Advisor
Posts: 4,173

Re: CHAR to NUMERIC

SAS has two data types: CHARACTER and NUMERIC

Data types determine how data gets stored and you can't change the type. You can only create a new column (variable), convert the values to the format of the new type, assign these values to the new column and then drop the old column.

 

Most of the time you don't need to go down this path. If you're only interested how the values of a variable "print" then use a format. No need to change the type.

 

Unlike the type and length of a variable, a format is an attribut which you can change via a modify statement.

 

If you tell us a bit more in detail what you have and what you need then we'll be able to give you the right advice.

Occasional Contributor
Posts: 7

Re: CHAR to NUMERIC

Hi all,

 

thanks a lot for your answers.

 

From your precious answers i can recap these logical steps:

 

1) rename the old variable

2) create a new variable with right format

3) put into the new variable the value of the old variable

4) drop the old variable

 

I still have difficult to understand how do this on my scenario:  I have X dataset which are used from X query into Visual Analytics.

The dataset is created by a script like the following one which read values from a csv and write ( in append mode ) on a dataset :

 

libname MyLibrary 'C:\LibFolder';
      data work.MyDataset    ;
      %let _EFIERR_ = 0; /* set the ERROR detection macro variable */
      infile 'C:\import\myfile.csv' delimiter = ';' MISSOVER DSD lrecl=32767 firstobs=2 ;
	     
         informat var1 yymmdd8.;
         informat var2 best32.;
         informat var3 $2.;
         informat var4 $1.;
         informat var5 $2.;
		 
         format var1 yymmdd8.;
         format var2 best32.;
         format var3 $2.;
         format var4 $1.;
         format var5 $2.;

         input
                var1 
	var2 
	var3 $
	var4 $
	var5 $

      ;
      if _ERROR_ then call symputx('_EFIERR_',1);  /* set ERROR detection macro variable */
      run;
	  
proc append base=MyLibrary.MyDataset data=work.MyDataset;
run;

 

 

If i do a a simply Select * from SAS compiler like the following one, i receive ERROR: La Libref MyLybrary is not assigned into the log

 

proc sql;
select * from MyLbrary.mydataset ;
quit;

 

 

So just to do the first step..... What i wrong ?

 

 

 

 

 

 

 

 

Super User
Super User
Posts: 7,039

Re: CHAR to NUMERIC

Sounds like a new question.

If you are getting an error that the library is not assigned then check the LIBNAME statement that created the library. Is the path you specified correct?  Is it visible from the computer that is running SAS? In many installations SAS is actually running on a Unix machine and directory name with C: at the beginning will never work on a Unix machine.

Super User
Posts: 11,343

Re: CHAR to NUMERIC


Library names may only have 8 characters. MyLibrary has 9 so in valid.

 

Also your error message:

 ERROR: La Libref MyLybrary  references a differently spelled library than your Libname statement (still too long), and the code used:

 

 select * from MyLbrary.mydataset ;

which has a different library than the error message, so there's something else missing from your displayed code.

 

 

 

 

 


 

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 518 views
  • 0 likes
  • 5 in conversation