DATA Step, Macro, Functions and more

Sas Log print

Reply
New User
Posts: 1

Sas Log print

Hi All,

I am totally new to SAS and i love to practice SAS. Since i have just started using sas i ran a small program.

 

 

Program

OPTIONS ERRORS=0 NOCENTER MISSING=' ';    
DATA RAM;                                 
  INFILE IN1;                             
  INFORMAT S_DATE YYMMDD6.;               
                                          
  INPUT @1 name $CHAR9.                   
        @396 S_DATE YYMMDD6.;             
                                          
  FORMAT S_DATE DATE9.;                   
                                          
  PROC PRINT;                             
  RUN;                                   

 

 

After running this i got the result but the output came in such a way date was printed as the first colummn and Name was printed as the second colum.

Can anyone suggest why? how can i get Name first and date as the second column in the out put.

 

Thanks in Advance,

Krish.

Super User
Posts: 3,115

Re: Sas Log print

Your date column S_DATE is first because it is in the INFORMAT statement before your INPUT statement. You don't actually need an INFORMAT statement as you supply the same information on your INPUT statement.

 

If you remove your INFORMAT statement NAME will  now come first. 

Trusted Advisor
Posts: 1,115

Re: Sas Log print

Welcome to the community, @Ramakrishnan!

 

The reason for the date being printed first is that variable S_DATE is the first variable mentioned in your program: in the INFORMAT statement. As a consequence, when the SAS compiler builds the structure of dataset WORK.RAM, it puts S_DATE at the first position in the so called program data vector (an internal area of memory).

 

Given this information, you already see various ways to change the order of the two variables:

For example, you could move the INFORMAT statement after the INPUT statement (so that the SAS compiler will first come across NAME as it processes your code). As a declarative statement it will still have its intended impact on the INPUT statement.

 

Actually, it would have an impact, if you didn't specify an informat for S_DATE in the INPUT statement as well. This second informat overrides the one specified in the INFORMAT statement. Since the two informats are identical anyway, I would simply delete the INFORMAT statement (thus, again, making NAME the first variable). This has an additional benefit: The YYMMDD6. informat will not be permanently associated with S_DATE and shown in the PROC CONTENTS output of the dataset. (Some people dislike such permanently associated informats.)

 

A third way to get NAME to the front is to insert a statement mentioning it before the INFORMAT statement. A LENGTH statement would be a good idea for a character variable such as NAME.

 

Finally, you can always modify the column order directly in the PROC PRINT step: by using a VAR statement; in this case:

var name s_date;

Regular Contributor
Posts: 161

Re: Sas Log print

[ Edited ]

In addition to the options mentioned by @SASKiwi and @FreelanceReinhard  you may also accomplish the order of the variables in your output by using a "PROC SQL" statement as shown in the examples below. 

 

OPTIONS ERRORS=0 NOCENTER MISSING=' ';    
DATA RAM;                                 
  INFILE datalines;   
  length  s_date $6. name $3.;              
  INPUT  name s_date ;
  datalines;
  abc 112915
  bcd 113015
  ;  
  DATA RAM;  set ram;
  n_date=input(s_date,mmddyy6.);
  run;                  
                                          
  PROC PRINT noobs;  format  n_date mmddyy10.;                    
  	title '==notice the order of the variables printed from proc print';
  RUN;  
  
  proc sql print;
    title '==notice the order of the variables printed from proc SQL print';
    select name, s_date, n_date format=mmddyy10. from ram;  	
  QUIT;  

The comparision of the output from "PROC PRINT" and "PROC SQL" are below for your reference : 

 

==notice the order of the variables printed from proc print
s_date	name	n_date
112915	abc	11/29/2015
113015	bcd	11/30/2015
==notice the order of the variables printed from proc SQL print
name	s_date	n_date
abc	112915	11/29/2015
bcd	113015	11/30/2015

 Hope this helps...Good Luck...!!!

Kannan Deivasigamani
Regular Contributor
Posts: 161

Re: Sas Log print

[ Edited ]

If you use a "Create Table" as part of your proc sql, the actual variables in your SAS dataset will be reordered which I forgot to mention.  I should say that it really "re-creates" the SAS dataset RAM using the "CREATE TABLE" statement with the variables in the order you have in the "SELECT" statement.

 

PROC SQL;

  CREATE TABLE RAM AS

     SELECT NAME, S_DATE, N_DATE FROM RAM;

QUIT;

 

If you run a "PROC CONTENTS" on RAM dataset before and after this SQL statement, you will notice the difference in the order of the variables in the SAS dataset using the VARNUM option as shown below:

 

PROC CONTENTS DATA=RAM VARNUM;

RUN;

 

VARNUM shows the output variables list in the same order as they were defined in the SAS dataset, else I believe it will be alphabetic.

 

Hope this helps.... Good Luck...!!!

Kannan Deivasigamani
Ask a Question
Discussion stats
  • 4 replies
  • 200 views
  • 0 likes
  • 4 in conversation