BookmarkSubscribeRSS Feed
daft22
Fluorite | Level 6

Hello!

I am importing a space delimited txt file that has several columns, among them a column which contains budget prices written in the following format ($10,000,000). I want to import this file making sure that this column is treated as a numeric value. I wrote the following code, however when I print the code the values are incomplete and don't show the full value as provided in the source file.

This is what is included in the source txt file. 
Numbers provided in this image include $1,344,000 ; $2,100,000 ; $2,000,000 ; $0 ; $1,400,000 .

 

SAS Example.png

 

This is the code I made:

DATA File;
INFILE '/folders/myfolders/sasuser.v94/File.txt' DSD DLM=' ' FIRSTOBS=2;
INPUT infodate :ANYDTDTE10. infotime infoyear info1 :$50. budget $ info2 info3 info4 info5 info6 info7;
RUN;

 

PROC PRINT DATA=File;

RUN;

 

I bolded the variable in question to make it easier for you to see what I did specifically.

These are the outputs I got from the PRINT Output.
NOTE: there is code I didn't include that actually ordered all the budget data from highest to lowest.

 

SAS Error 2.png

 

Any and all help is appreciated!

Thank you!

2 REPLIES 2
Norman21
Lapis Lazuli | Level 10

You can use the COMMA. informat, which will "ignore" the dollar sign and input the data as a number

 

If you want the dollar sign to re-appear on output, use the DOLLARw.d format.

Norman.
SAS 9.4 (TS1M6) X64_10PRO WIN 10.0.17763 Workstation

Tom
Super User Tom
Super User

You didn't define a length for your character variable BUDGET.  And you didn't give it any hints by including an informat with a width that SAS could use to guess what length to use, like you did for INFO1.  So BUDGET got created with a length of $8.

 

If you want to read it as a NUMBER then use a numeric informat when reading it. The COMMA informat will ignore $ and commas and treat the resulting string as a number.

input infodate :anydtdte. infotime infoyear info1 :$50. budget :comma. info2-info7;

If you want the numbers to display with dollar signs and commas then attach the DOLLAR format to the variable. Either in this step or when you are outputting it.

format budget dollar16.;

Note there is no need to specify a width on the informats when using list mode input.  The input statement will ignore the width specified and read all of the characters in the next value in the input stream.  The only reason you need the $50. informat for INFO1 is that you didn't define the variable before the INPUT statement so without any hints SAS will use default length of $8 for character variables.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 548 views
  • 0 likes
  • 3 in conversation