DATA Step, Macro, Functions and more

Formats and Informats

Reply
Occasional Contributor
Posts: 5

Formats and Informats

I have a piece of code that has infile, informat then input steps after which allocations happens on the file that has been imported.

However, one of the fields is read in as informat C_C $21. and input C_C $

In the output file, the field is cut off and doesn't have enough characters, I am not sure how to change the format for the output to not cut off. i have tried adding format straight after informat but before input, however, this doens't make any difference.

 

data CHARGE_DETAIL_&billing_month;
    %let _EFIERR_ = 0; /* set the ERROR detection macro variable */
 infile "&path\MCB_Raw\charge_detail_&billing_month..csv"
 delimiter = ',' MISSOVER DSD lrecl=32767 firstobs=2 ;
        informat c_c $21.;
        informat cust_name $35.
    input
        c_c $
        cust_name $
    ;

    if _ERROR_ then call symputx('_EFIERR_',1);  /* set ERROR detection macro variable */
    run;

data CHARGE_DETAIL_&billing_month(drop= billing_monthx billing_year);
format cust_name $35. billing_month ddmmyy10.;
    set CHARGE_DETAIL_&billing_month(rename=(billing_month=billing_monthx));
billing_month = input('01'||billing_monthx||compress(billing_year), date9.);
proc sort data= CHARGE_DETAIL_&billing_month;
by billing_month;
run;
Super User
Posts: 6,936

Re: Formats and Informats

Your code can't really work as posted; the second informat statement lacks a closing semicolon.

Please post the real code you ran, the log, and some lines from the infile that illustrate what is being cut off.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 5

Re: Formats and Informats

Apologies,

 

It has a semicolon. The code runs successfully, but cuts this field short.

data CHARGE_DETAIL_&billing_month;
    %let _EFIERR_ = 0; /* set the ERROR detection macro variable */
infile "&path\MCB_Raw\charge_detail_&billing_month..csv"
delimiter = ',' MISSOVER DSD lrecl=32767 firstobs=2 ;
		informat c_c $21.;
		informat cust_name $35.;
	input
		c_c $
		cust_name $
	;

    if _ERROR_ then call symputx('_EFIERR_',1);  /* set ERROR detection macro variable */
    run;
data CHARGE_DETAIL_&billing_month(drop= billing_monthx billing_year);
format cust_name $35. billing_month ddmmyy10.;
	set CHARGE_DETAIL_&billing_month(rename=(billing_month=billing_monthx));
billing_month = input('01'||billing_monthx||compress(billing_year), date9.);
proc sort data= CHARGE_DETAIL_&billing_month;
by billing_month;
run;
Super User
Posts: 5,082

Re: Formats and Informats

[ Edited ]

Before the INPUT statement, try adding:

 

length c_c $ 21;

 

If that doesn't fix it, it's because the data itself contains commas that should become part of C_C.  That would require a different change.

Occasional Contributor
Posts: 5

Re: Formats and Informats

Thanks,

 

I have tried both format and length before input function and it also doens't solve the issue

Super User
Posts: 6,936

Re: Formats and Informats

Without some example data lines from the infile, we cannot test the code.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Regular Contributor
Posts: 216

Re: Formats and Informats

Try changing  informat c_c $21.; to informat c_c $100.; 

Super User
Super User
Posts: 6,500

Re: Formats and Informats

The second data step is referencing variables that your first data step that reads the CSV file is not creating. 

Perhaps your real problem lies there and not in the reading of the text file.

 

Your import step looks like it was either generated by PROC IMPORT or copied from the code that PROC IMPORT generates.  The code that PROC IMPORT generates generally works, but it is not really very good code to use for modeling your own data step. You do not need to specify either an INFORMAT or a FORMAT for character variable. Just define the length that you want them to have.  In general you only need INFORMAT for Date/Time variables or numeric variables that include commas and dollar signs in the text file.  You only need to attach FORMATs to Date/TIme varaibles. You can attach formats to numeric variable if you want to make the values display better for you.  For example if your values are large you might want to use COMMA (or DOLLAR) format so that it will be easier to users to read the results when SAS prints the values.

 

data CHARGE_DETAIL_&billing_month;
  infile "&path\MCB_Raw\charge_detail_&billing_month..csv"
    DSD DLM=',' TRUNCOVER lrecl=32767 firstobs=2 
  ;
  length c_c $21 cust_name $35 ;
  input c_c -- cust_name ;
run;
Ask a Question
Discussion stats
  • 7 replies
  • 348 views
  • 0 likes
  • 5 in conversation