BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Jarvin99
Obsidian | Level 7

Question 1:

I have an attached 'try' txt file, which is split by the symbol |. As a rookie, I want to import it nicely into the SAS but not sure how to do it.

Each row should have 21 columns. Hence, some rows may have missing values in some columns.

 

Question 2:

After I import the txt file successfully, how do I add a header, say for 21 columns, to this new SAS data? The header I wanna add is also attached here with the name "header".

 

Thank you so much.

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

You do not want to change the length of a numeric variable, as that concerns the number of bytes used to store the numbers. What you want to change is the display format for the numeric values.

To reliably display up to 14 digits plus 2 fractional digits, use a format of 18.2.

18 = 14 + 1 (dot) + 1 (sign) + 2 (fractional digits)

In the code I gave you, extend the FORMAT statement:

format
  TRANSACTION_DT yymmdd10.
  TRANSACTION_AMT 18.2
;

Note that a number with more than 15 overall decimal digits will have imprecisions in the last digits, because of the limits of 8-byte real storage.

View solution in original post

4 REPLIES 4
Kurt_Bremser
Super User

Use the header line to write your code, and use informats according to the data.

But, in a professional environment, you should accept files only with proper documentation, so you do not have to make guesses about variable attributes.

Anyway, this seems to read your file correctly:

data want;
infile "~/try.txt" dlm="|" dsd truncover;
input
  CMTE_ID :$10.
  AMNDT_IND :$1.
  RPT_TP :$2.
  TRANSACTION_PGI :$5.
  IMAGE_NUM :$20.
  TRANSACTION_TP :$3.
  ENTITY_TP :$3.
  NAME :$50.
  CITY :$50.
  STATE :$2.
  ZIP_CODE :$10.
  EMPLOYER :$50.
  OCCUPATION :$30.
  TRANSACTION_DT :mmddyy8.
  TRANSACTION_AMT
  OTHER_ID :$8.
  TRAN_ID :$20.
  FILE_NUM :$8.
  MEMO_CD :$1.
  MEMO_TEXT :$100.
  SUB_ID :$20.
;
format TRANSACTION_DT yymmdd10.;
run;
Jarvin99
Obsidian | Level 7

Thank you for your reminder. Here is a brief manual from the database. I have changed most of the character formats, but I am not sure how to change the numeric format, i.e., TRANSACTION_AMT from its default length of 8 to 14 with 2 decimals behind.

 

I know it may be simple but thanks for any help given.

AMSAS
SAS Super FREQ

Note Length and Format are not the same, you really don't want to mess with the length of numeric variables
see Length and Precision of Variables 

A format is how SAS displays data
see About Formats 

Kurt_Bremser
Super User

You do not want to change the length of a numeric variable, as that concerns the number of bytes used to store the numbers. What you want to change is the display format for the numeric values.

To reliably display up to 14 digits plus 2 fractional digits, use a format of 18.2.

18 = 14 + 1 (dot) + 1 (sign) + 2 (fractional digits)

In the code I gave you, extend the FORMAT statement:

format
  TRANSACTION_DT yymmdd10.
  TRANSACTION_AMT 18.2
;

Note that a number with more than 15 overall decimal digits will have imprecisions in the last digits, because of the limits of 8-byte real storage.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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