DATA Step, Macro, Functions and more

Permanent Format Statement

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 13
Accepted Solution

Permanent Format Statement

Hello, 

 

my understanding about FORMAT in SAS is if you add the format statement in DATA step, it creates permanent format. If the format statement is added to PROC print step, it only creates the format on the output.  However, when I write the following programs, the format for the variable" HireDate" didn't change, it still appears as SAS data value.  But when I move the format statement to PROC PRINT step, it shows correctly.  Why is that?  Did I miss anything?  Thanks for your help.

 

data work.sales;
infile datalines dlm="/";
input ID First :$12. Last :$12. Gender $ Salary:comma. Title :$25. HireDate :date.;
datalines;
120102/Tom/Zhou/M/108,255/Sales Manager/01Jun1993
120103/Wilson/Dawes/M/87,975/Sales Manager/01Jan1978
120261/Harry/Highpoint/M/243,190/Chief Sales Officer/01Aug1991
121143/Louis/Favaron/M/95,090/Senior Sales Manager/01Jul2001
121144/Renee/Capachietti/F/83,505/Sales Manager/01Nov1995
121145/Dennis/Lansberry/M/84,260/Sales Manager/01Apr1980
;
format HireDate mmddyy10.;
run;

Title "Orion Star Management Team";
proc print data=work.sales;
run;
Title;

Capture.JPG


Accepted Solutions
Solution
‎01-15-2018 09:03 PM
Super Contributor
Posts: 266

Re: Permanent Format Statement

Try it like this:

data work.sales;
infile datalines dlm="/";
input ID First :$12. Last :$12. Gender $ Salary:comma. Title :$25. HireDate :date.;
format HireDate mmddyy10.;
datalines;
120102/Tom/Zhou/M/108,255/Sales Manager/01Jun1993
120103/Wilson/Dawes/M/87,975/Sales Manager/01Jan1978
120261/Harry/Highpoint/M/243,190/Chief Sales Officer/01Aug1991
121143/Louis/Favaron/M/95,090/Senior Sales Manager/01Jul2001
121144/Renee/Capachietti/F/83,505/Sales Manager/01Nov1995
121145/Dennis/Lansberry/M/84,260/Sales Manager/01Apr1980
;
run;
Title "Orion Star Management Team";
proc print data=work.sales;
run;
Title;

When you use 'cards' or 'datelines', it should be the last command in a data step. Your code as it was was reporting an error.

View solution in original post


All Replies
Solution
‎01-15-2018 09:03 PM
Super Contributor
Posts: 266

Re: Permanent Format Statement

Try it like this:

data work.sales;
infile datalines dlm="/";
input ID First :$12. Last :$12. Gender $ Salary:comma. Title :$25. HireDate :date.;
format HireDate mmddyy10.;
datalines;
120102/Tom/Zhou/M/108,255/Sales Manager/01Jun1993
120103/Wilson/Dawes/M/87,975/Sales Manager/01Jan1978
120261/Harry/Highpoint/M/243,190/Chief Sales Officer/01Aug1991
121143/Louis/Favaron/M/95,090/Senior Sales Manager/01Jul2001
121144/Renee/Capachietti/F/83,505/Sales Manager/01Nov1995
121145/Dennis/Lansberry/M/84,260/Sales Manager/01Apr1980
;
run;
Title "Orion Star Management Team";
proc print data=work.sales;
run;
Title;

When you use 'cards' or 'datelines', it should be the last command in a data step. Your code as it was was reporting an error.

Occasional Contributor
Posts: 13

Re: Permanent Format Statement

Hi Laurie, 

 

Thanks for your help!  Smiley Happy 

 

Fiery 

Super User
Super User
Posts: 7,935

Re: Permanent Format Statement

[ Edited ]

If you want the FORMAT statement to be part of the data step then include it in the data step, not after it. You cannot have any statements for the data step after the in-line data lines.  You should have gotten an error message like this in the LOG. 

112  ;
113  format HireDate mmddyy10.;
     ------
     180

ERROR 180-322: Statement is not valid or it is used out of proper order.

114  run;

Like this:

data work.sales;
  infile datalines dlm="/";
  input ID First :$12. Last :$12. Gender $ Salary:comma. Title :$25. HireDate :date.;
  format HireDate mmddyy10.;
datalines;
120102/Tom/Zhou/M/108,255/Sales Manager/01Jun1993
120103/Wilson/Dawes/M/87,975/Sales Manager/01Jan1978
120261/Harry/Highpoint/M/243,190/Chief Sales Officer/01Aug1991
121143/Louis/Favaron/M/95,090/Senior Sales Manager/01Jul2001
121144/Renee/Capachietti/F/83,505/Sales Manager/01Nov1995
121145/Dennis/Lansberry/M/84,260/Sales Manager/01Apr1980
;

 

Occasional Contributor
Posts: 13

Re: Permanent Format Statement

Hi Tom, 

 

Thanks!  I saw the error, since I didn't know that CARDS/DATALINES needs to be the last statement in the DATA step, I couldn't figure out what was wrong. Now I know! 

 

Thanks for your time.  Smiley Happy 

 

Fiery 

Super User
Posts: 23,311

Re: Permanent Format Statement

Statements after the CARDS/DATALINE statement are assumed to be data or are otherwise ignored. 

So in this case the location of your FORMAT statement matters, it usually doesn't. 


Fiery wrote:

Hello, 

 

my understanding about FORMAT in SAS is if you add the format statement in DATA step, it creates permanent format. If the format statement is added to PROC print step, it only creates the format on the output.  However, when I write the following programs, the format for the variable" HireDate" didn't change, it still appears as SAS data value.  But when I move the format statement to PROC PRINT step, it shows correctly.  Why is that?  Did I miss anything?  Thanks for your help.

 

data work.sales;
infile datalines dlm="/";
input ID First :$12. Last :$12. Gender $ Salary:comma. Title :$25. HireDate :date.;
datalines;
120102/Tom/Zhou/M/108,255/Sales Manager/01Jun1993
120103/Wilson/Dawes/M/87,975/Sales Manager/01Jan1978
120261/Harry/Highpoint/M/243,190/Chief Sales Officer/01Aug1991
121143/Louis/Favaron/M/95,090/Senior Sales Manager/01Jul2001
121144/Renee/Capachietti/F/83,505/Sales Manager/01Nov1995
121145/Dennis/Lansberry/M/84,260/Sales Manager/01Apr1980
;
format HireDate mmddyy10.;
run;

Title "Orion Star Management Team";
proc print data=work.sales;
run;
Title;

Capture.JPG


 

 

 

Super User
Posts: 9,914

Re: Permanent Format Statement

Maxim 2: Read the Log. The ERROR message would have alerted you to the mistake in your code.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Occasional Contributor
Posts: 13

Re: Permanent Format Statement

Posted in reply to KurtBremser

Hi Kurt, 

 

Thanks for sharing this link, it is indeed very useful for a fresh leaner like me. I am teaching myself by using internet. It is possible sometime miss out some key points. It is great thing to have the community, so I can get helps from experts like you guys. 

 

Thank you! 

 

Fiery 

☑ This topic is solved.

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

Discussion stats
  • 7 replies
  • 187 views
  • 5 likes
  • 5 in conversation