DATA Step, Macro, Functions and more

format question?

Accepted Solution Solved
Reply
Super Contributor
Posts: 319
Accepted Solution

format question?

Hello:

 

I tried to import a '.csv' file into SAS.  I found some codes from the previous programmer as the list below.  I have some questions:

 

1.  Why use "informat" first then "format" statement?  I tried to just use the "format" statement.  I don't see any difference.

2.  After the file was imported, I found the order of the columns has been changed.  It changed to "JL Rating1 Rating2 Rating3 Rating4 RHSC HTLC LRC VS".  Due to the rating is corresponded to its previous column name,  how to keep the order the same as input order "JL $ RHSC $ Rating1 $ HTLC $ Rating2 $ LRC $ Rating3 $ VS $ Rating4 $"?  

 

Thanks.

Y

 

 

data test;

      infile test MISSOVER DSD firstobs=2;

      informat JL $22. ;

      informat rating1 $32. ;

      informat rating2 $18. ;

      informat rating3 $100. ;

      informat rating4 $12. ;

      format JL $22. ;

      format rating1 $32. ;

      format rating2 $18. ;

      format rating3 $100. ;

      format rating4 $12. ;

      input JL $ RHSC $ Rating1 $ HTLC $ Rating2 $ LRC $ Rating3 $ VS $ Rating4 $;

run;


Accepted Solutions
Solution
‎05-10-2017 09:30 PM
Super User
Posts: 11,343

Re: format question?

The appearance for the code makes me believe that may have been generated by proc import.

 

With simple character variables as long as the length of the variable is set before reading with either informat, format or attrib statement there isn't much difference. However for things such as Date, Datetime or Time variables you would have significantly different behavior as if SAS does not know how to read one of those then applying the format will likely result in something quite different.

Consider these two reads:

 

data example;
   input date;
   format date date9.;
datalines;
01012017
;
run;

data example2;
   informat date mmddyy8.;
   input date;
   format date date9.;
datalines;
01012017
;
run;

View solution in original post


All Replies
Solution
‎05-10-2017 09:30 PM
Super User
Posts: 11,343

Re: format question?

The appearance for the code makes me believe that may have been generated by proc import.

 

With simple character variables as long as the length of the variable is set before reading with either informat, format or attrib statement there isn't much difference. However for things such as Date, Datetime or Time variables you would have significantly different behavior as if SAS does not know how to read one of those then applying the format will likely result in something quite different.

Consider these two reads:

 

data example;
   input date;
   format date date9.;
datalines;
01012017
;
run;

data example2;
   informat date mmddyy8.;
   input date;
   format date date9.;
datalines;
01012017
;
run;
PROC Star
Posts: 7,474

Re: format question?

Also, in answer to your other question, the informat and format statements, being before the input statement, control the order of the variables in the resulting file and it works from top to bottom the first time a variable is used. So, all of the variables that you had informats for will be the first variables in your resulting file, followed by those that weren't mentioned in your combination of informat and format statements.

 

Art, CEO, AnalystFinder.com

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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