Help using Base SAS procedures

Changing the length, format, informat of a character variable

Reply
Occasional Contributor
Posts: 11

Changing the length, format, informat of a character variable

[ Edited ]

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!

Super User
Posts: 6,785

Re: Changing the length, format, informat of a character variable

[ Edited ]

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).

 

Super User
Posts: 13,583

Re: Changing the length, format, informat of a character variable


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.

Occasional Contributor
Posts: 11

Re: Changing the length, format, informat of a character variable

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!

Super User
Super User
Posts: 8,127

Re: Changing the length, format, informat of a character variable

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

Ask a Question
Discussion stats
  • 4 replies
  • 355 views
  • 0 likes
  • 4 in conversation