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-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
  • 4 replies
  • 14159 views
  • 0 likes
  • 4 in conversation