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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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