Help using Base SAS procedures

Length statement and truncover option

Reply
Contributor
Posts: 27

Length statement and truncover option

can length statements be used instead of truncover option in raw data sets

Respected Advisor
Posts: 3,799

Re: Length statement and truncover option

Posted in reply to NoorulIyn

Are oranges better than apples?

Contributor
Posts: 27

Re: Length statement and truncover option

Posted in reply to data_null__

I am new into SAS and can someone explain this both.Thanks

Super User
Super User
Posts: 7,060

Re: Length statement and truncover option

Posted in reply to NoorulIyn

They are not really even related.

The TRUNCOVER (vs FLOWOVER) option sets how SAS behaves when you read past the end of the line.

The LENGTH option lets you tell SAS what variable to populate with the length of the line.

You could conceivably generate some customized logic using the values of the variables specified by the LENGTH and COLUMN options to control when to move to another line, but 99.9 % of normal data input would not require that.

Contributor
Posts: 27

Re: Length statement and truncover option

Thanks !!!

Can you suggest another option for this program used in UE edition to produce sames results without using length statement

data work.sales2;

  length Employee_ID 8 First_Name $ 12

  Last_Name $ 18 Gender $ 1

   Salary 8 Job_Title $ 25

  Country $ 2;

  infile "&path/sales.csv" dlm=',';

  input Employee_ID First_Name $ Last_Name $

  Gender $ Salary Job_Title $ Country $;

run;

How can I get the same result without truncated job_title....

Thanks In Advance.....

Super User
Posts: 19,822

Re: Length statement and truncover option

Posted in reply to NoorulIyn

Your code is missing reading in the last two variables which is possibly causing some of the issues..

You should also use the DSD option instead of DLM=',' as well as the truncover option.

data work.sales2;

  length Employee_ID $8. First_Name $12.

  Last_Name $18. Gender $.1

   Salary 8. Job_Title $25. Date1 date9. date2 mmddyyyy10.

  Country $2.;

  infile "&path/sales.csv" DSD Truncover;

  input Employee_ID First_Name  Last_Name Gender Salary Job_Title  Country date1 date2;

run;

Super User
Posts: 19,822

Re: Length statement and truncover option

Posted in reply to NoorulIyn

Without seeing what your data looks like its difficult to say why the job title is being truncated.

Does it have spaces or is it just longer than 25 characters?

Contributor
Posts: 27

Re: Length statement and truncover option

v1 length 20 reading the same line

1120102TomZhouM108255Sales MaAU
2120103WilsonDawesM87975Sales MaAU
3120121IrenieElvishF26600Sales ReAU
4120122ChristinNganF27475Sales ReAU
5120123KimikoHotstoneF26190Sales ReAU
6120124LucianDaymondM26480Sales ReAU
7120125FongHofmeistM32040Sales ReAU
8120126SatyakamDennyM26780Sales ReAU
9120127SharrynClarksonF28100Sales ReAU

without length option.....

data csv file

120102,Tom,Zhou,M,108255,Sales Manager,AU,11AUG1973,06/01/1993                 

120103,Wilson,Dawes,M,87975,Sales Manager,AU,22JAN1953,01/01/1978              

120121,Irenie,Elvish,F,26600,Sales Rep. II,AU,02AUG1948,01/01/1978             

120122,Christina,Ngan,F,27475,Sales Rep. II,AU,27JUL1958,07/01/1982            

120123,Kimiko,Hotstone,F,26190,Sales Rep. I,AU,28SEP1968,10/01/1989            

120124,Lucian,Daymond,M,26480,Sales Rep. I,AU,13MAY1963,03/01/1983             

120125,Fong,Hofmeister,M,32040,Sales Rep. IV,AU,06DEC1958,03/01/198

Super User
Super User
Posts: 7,060

Re: Length statement and truncover option

Posted in reply to NoorulIyn

The LENGTH statement is a method for defining your variables. You can also use the ATTRIB statement.

Once you have defined your variables the INPUT statement is much easier to write.  In fact you can even use variable lists as in the example below.

The TRUNCOVER option will allow the INPUT statement to handle lines when the last value is empty, or even if there are not enough delimiters on the line.  For example see how I changed the next to last line of input data.

Your example data looks like a normal CSV file. So you should use the DSD option.

data want ;

  infile cards dsd truncover ;

  length

     Employee_ID 8

     First_Name $ 12

     Last_Name $ 18

     Gender $ 1

     Salary 8

     Job_Title $ 25

     Country $ 2

     Date1 8

     Date2 8

  ;

  informat date1 date9. date2 mmddyy10. ;

  format date1 date2 yymmdd10.;

  input Employee_ID -- Date2 ;

cards;

120102,Tom,Zhou,M,108255,Sales Manager,AU,11AUG1973,06/01/1993

120103,Wilson,Dawes,M,87975,Sales Manager,AU,22JAN1953,01/01/1978             

120121,Irenie,Elvish,F,26600,Sales Rep. II,AU,02AUG1948,01/01/1978

120122,Christina,Ngan,F,27475,Sales Rep. II,AU,27JUL1958,07/01/1982

120123,Kimiko,Hotstone,F,26190,Sales Rep. I,AU,28SEP1968,10/01/1989

120124,Lucian,Daymond,M,26480,Sales Rep. I,AU,13MAY1963           

120125,Fong,Hofmeister,M,32040,Sales Rep. IV,AU,06DEC1958,03/01/1983

;;;;

Super User
Super User
Posts: 7,060

Re: Length statement and truncover option

Posted in reply to NoorulIyn

I suspect that your question is triggered by confusion between using formatted input and list mode input when writing your INPUT statement rather than the TRUNCOVER option of the INFILE statement. Take a look at this page:  SAS(R) 9.2 Language Reference: Concepts, Second Edition

The description of LIST INPUT style could use a little clarification:

  • The example INPUT statement has an unneeded $.  Because the variable has already been defined there is no need to include the $ in the INPUT statement.
  • The statement about blanks not representing a missing value only applies when you are using blank as your delimiter. Also look at the effect of modifiers such as & that are mentioned in the MODIFIED LIST INPUT style section.
  • They do not explain how the defined INFORMAT impacts the type data that can be read.  If you have defined an INFORMAT for your variables then you can read them with LIST INPUT style and the informat assigned to the variables in the list will automatically be read.
Contributor
Posts: 27

Re: Length statement and truncover option

Thanks!Now I  understood clearly....

Ask a Question
Discussion stats
  • 10 replies
  • 294 views
  • 9 likes
  • 4 in conversation