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

Hi, there! I am still becoming familiar with programming in SAS Studio 3.1, and am currently in the process of getting SAS to read CSV files that were created from Excel data spreadsheets. The file I'm using ("MyData_AMV.csv") is being imported and read as a table (work.MyData), which is great! But the values in the columns containing character string variables are being truncated to 8 characters, and I want to specify certain columns as having more characters in a string. For example, I want the "LastName" column to have a length of up to 20 characters.

 

 

data work.MyData;
	infile 'C:\\Users\Public\Documents\MyData_AMV.csv'
		dlm=',' firstobs=2;
	input StudentID LastName $ FirstName $ Age Gender $ Race $ InstitutionType $ InstitutionName $ 
HighSchoolGPA Term $ CourseSection $ LetterGrade $ CurrentStatus $; run;

Any suggestions on how to fix this problem would be much appreciated!

 

Thank you!

~AMV

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

SAS defaults to 8 characters for unspecified input.

Use an INFORMAT statement BEFORE the variable is input to assign length is easiest.

data work.MyData;
	infile 'C:\\Users\Public\Documents\MyData_AMV.csv'
		dlm=',' firstobs=2;
   informat LastName $20.;
   Informat FirstName $20.;
	input StudentID LastName $ FirstName $ Age Gender $ Race $ InstitutionType $ InstitutionName $ 
                HighSchoolGPA Term $ CourseSection $ LetterGrade $ CurrentStatus $;
run;

One hint for the future, you can use proc import with CSV and provide a largish GUESSINGROWS option value. Proc Import actually creates Datastep code that you can copy fromt the log and edit for anything you want to change.

 

View solution in original post

3 REPLIES 3
ballardw
Super User

SAS defaults to 8 characters for unspecified input.

Use an INFORMAT statement BEFORE the variable is input to assign length is easiest.

data work.MyData;
	infile 'C:\\Users\Public\Documents\MyData_AMV.csv'
		dlm=',' firstobs=2;
   informat LastName $20.;
   Informat FirstName $20.;
	input StudentID LastName $ FirstName $ Age Gender $ Race $ InstitutionType $ InstitutionName $ 
                HighSchoolGPA Term $ CourseSection $ LetterGrade $ CurrentStatus $;
run;

One hint for the future, you can use proc import with CSV and provide a largish GUESSINGROWS option value. Proc Import actually creates Datastep code that you can copy fromt the log and edit for anything you want to change.

 

vagnozzia408
Calcite | Level 5

That fixed the problem! Thanks!

~AMV

Tom
Super User Tom
Super User

I would use a LENGTH or ATTRIB statement to set the length of a variable.  

 

The INFORMAT statement is for attaching the INFORMAT, it will only set the length as a side effect.  Now if you do not want SAS to use its default informat behaviour then you could use an INFORMAT statement. Such as if you wanted to read a date or time value.  Of if you wanted to use the $CHAR informat to preserve leading spaces.

 

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 3 replies
  • 3188 views
  • 2 likes
  • 3 in conversation