BookmarkSubscribeRSS Feed
leigao
Calcite | Level 5

Use the Data Step to create a SAS dataset based on the information contained in the file. All variables in the SAS dataset should have the numeric data type. The 'gasbill' variable would be missed if I use fixed-width 15-16 for 'bilingDays'. Without using the fixed-width the data would be read correctly. I want to know why this happens. 

data gas;
infile 'Database/gas_bills.txt'
dlm=';' truncover;
input 
	date mmddyy10. +1
	temp 2.
	blingDays 15-16
	gasbill dollar16.
	;
run;
2 REPLIES 2
Reeza
Super User

Why would you used fixed width to read a file that has clear delimiters?

If you want to separate the temp and blingDays have you tried including multiple delimiters, both the colon and a space in the DLM option? You'll also want to specify DSD option. 

 


@leigao wrote:

Use the Data Step to create a SAS dataset based on the information contained in the file. All variables in the SAS dataset should have the numeric data type. The 'gasbill' variable would be missed if I use fixed-width 15-16 for 'bilingDays'. Without using the fixed-width the data would be read correctly. I want to know why this happens. 

data gas;
infile 'Database/gas_bills.txt'
dlm=';' truncover;
input 
	date mmddyy10. +1
	temp 2.
	blingDays 15-16
	gasbill dollar16.
	;
run;

 

data gas;
infile '..gas_bills.txt' dlm = '; ' truncover;
informat date mmddyy. temp blingDays 8. gasbill dollar.;
format date mmddyy. temp blingDays 8. gasbill dollar16.2;
input date temp blingDays gasbill;
run;



 

Tom
Super User Tom
Super User

Do not mixed FORMATTED and LIST MODE without knowing what you are doing.

Just because the file has semi-colons in it does not mean you have to treat it as delimited.

data gas;
  infile cards truncover;
  input date mmddyy10.
     +1 temp 2.
     +1 blingDays 2.
     +1 gasbill :comma.
  ;
  format date yymmdd10.;
cards4;
12/29/1999;26 36;$112.72
 1/28/2000;18 30;$95.88
 2/26/2000;24 29;$134.65
 3/25/2000;41 28;$15.32
 4/28/2000;45 34;$47.33
 5/30/2000;60 32;$89.87
 6/24/2000;66 25;$25.55
 7/26/2000;72 32;$8.08
 8/24/2000;72 29;$17.66
;;;;

This works for your example because the fields are all fixed length.

Screenshot 2021-12-30 180051.jpg

So you could also read them using column mode input , except for fields like DATE and GASBILL that require special informat.  For those use formatted mode, you can use @ cursor motion to locate where to start reading GASBILL.

  input date mmddyy10.
        temp 12-13
        blingDays 15-16
        @18 gasbill :comma.
  ;

Or you could read it as delimited.  But since you have two values in one field read that as text and then parse it into the two values.

data gas;
  infile cards dsd dlm=';' truncover;
  input date :mmddyy. string $ gasbill :comma.;
  temp=input(string,2.);
  blingDays=input(scan(string,2,' '),32.);
  format date yymmdd10.;
  drop string;
cards4;
12/29/1999;26 36;$112.72
 1/28/2000;18 30;$95.88
 2/26/2000;24 29;$134.65
 3/25/2000;41 28;$15.32
 4/28/2000;45 34;$47.33
 5/30/2000;60 32;$89.87
 6/24/2000;66 25;$25.55
 7/26/2000;72 32;$8.08
 8/24/2000;72 29;$17.66
;;;;

 

Results

                             bling
Obs          date    temp     Days    gasbill

 1     1999-12-29     26       36      112.72
 2     2000-01-28     18       30       95.88
 3     2000-02-26     24       29      134.65
 4     2000-03-25     41       28       15.32
 5     2000-04-28     45       34       47.33
 6     2000-05-30     60       32       89.87
 7     2000-06-24     66       25       25.55
 8     2000-07-26     72       32        8.08
 9     2000-08-24     72       29       17.66

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1122 views
  • 2 likes
  • 3 in conversation