BookmarkSubscribeRSS Feed
StefB
Calcite | Level 5

Hi there,

I imported a .csv file and it changed my length, format, and informat for one of my variables.  How do I change that back to $40.?  It is for my lab test variable and when it imported it only did a character length of $2..  Thank you!

4 REPLIES 4
Astounding
PROC Star

You can use a LENGTH statement to change the length ... but you have to insert it before the SET statement:

 

data want;

length labtest $ 40;

set have;

format labtest;

informat labtest;

run;

 

Since the data is now in a SAS data set, it is probably easiest to just remove any formats or informats (as illustrated above as well).

 

ballardw
Super User

@StefB wrote:

Hi there,

I imported a .csv file and it changed my length, format, and informat for one of my variables.  How do I change that back to $40.?  It is for my lab test variable and when it imported it only did a character length of $2..  Thank you!


What "it" changed the length of your variable?

If you use proc import to read data it guesses as the variable type, length and in some cases the informat and format of variables. Without specific instructions it only examines a very few rows of data and may miss the longer variables.

 

If you are using a wizard or tool to import the data look for the setting on the number of rows to examine for setting this information If using Proc Import code then add as statement: guessingrows=max; to the import code.

Then see if that is correct.

If the data were read as $2 then making the length longer will not recover your values from the 3, 4, 5th or further positions. Everything was truncated. So you likely need to reimport your data.

Note that Proc Import will create data step code to read your file. Look in the log. You can copy that from the log and edit to set lengths, informats, formats and such to what ever you need.

StefB
Calcite | Level 5

Thanks for replying!

"It" was my .csv file.  Let me start off by saying I am super new to SAS so I apologize if I am not explaining things correctly. Let me start from the beginning. I imported my .csv file and I noticed that some variables were shortened compared to my .xlsx file that I was using previously.  Before when I imported the other .xlsx file my  labtest variable displayed the entire test name.  After, with the .csv file it only displayed the first two letters of the lab test.  I looked at the column attributes in the original file and the length, format and informat was $40., but in the .csv version its $2..

 

I added guessingrows=max; into the import code and my column attributes for labtest and they are back to $40.  After running the proc import I noticed other variables need to be edited, but how do I do that? 

 

Thanks again!

Tom
Super User Tom
Super User

If your source file is a CSV file then just read it with your own data step. That way there is no guessing involved.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 4 replies
  • 15248 views
  • 0 likes
  • 4 in conversation