BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Davi3
Fluorite | Level 6

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.

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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

6 REPLIES 6
ballardw
Super User

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.

pearsoninst
Pyrite | Level 9
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.
Patrick
Opal | Level 21

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.

Davi3
Fluorite | Level 6

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 ?

 

 

 

 

 

 

 

 

Tom
Super User Tom
Super User

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.

ballardw
Super User

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.

 

 

 

 

 


 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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