BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ybz12003
Rhodochrosite | Level 12

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;

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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

2 REPLIES 2
ballardw
Super User

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;
art297
Opal | Level 21

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

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 2 replies
  • 1125 views
  • 2 likes
  • 3 in conversation