Solved

Where to place counts in SAS code?

Posted on 2013-02-06
8
600 Views
Last Modified: 2013-11-16
I am trying to write counts to mylog.log file for file allrecords.dat (read from counts) and files file1.dat and file2.dat (written to counts).  
Where do I place writtencnt1+1 and writtencnt2+1 counts in code for file1.dat and file2.dat?

Note: readcnt+1 count is already placed for allrecords.dat file and it is working fine.  

DATA _NULL_;
 drop readcnt writtencnt1 writtencnt2; 
 infile "allrecords" end=lastrec;
   length readcnt  8 writtencnt1 8 writtencnt2 8;
   retain readcnt  0 writtencnt1 0 writtencnt2 0;
  input @1 Var1 $char20.;

  IF SUBSTR(Var1,1,1) = '1'  then File "h:\xxx\test1.txt";
   else IF SUBSTR(Var1,1,1) = '2' THEN File "h:\xxx\test2.txt";

  readcnt+1;
   PUT _INFILE_;

if lastrec then do;
          file mylog.log; 
         
          /***************************
          * Records read from allrecords.dat
          ***************************/
          put "Total records read " readcnt;
         
          /********************************
          * Records written to test1.dat
          ********************************/
          put "Total records written " writtencnt1;
         
          /********************************
          * Records written to test2.dat
          ********************************/
          put "Total records written " writtencnt2;
         
         end;
 run;
RUN;

Open in new window


Any examples, comments or suggestions will be greatly appreciated!
0
Comment
Question by:labradorchik
  • 4
  • 3
8 Comments
 
LVL 14

Expert Comment

by:Aloysius Low
ID: 38860294
first, try declaring your FILE statement before the DATA step.

if it is not required to achieve all in one step, then i would say step 1 is to read the data into 2 datasets, splitting them as you read. keep track of the records read and store into a macro variable

then step 2 is to output the contents of the 2 datasets produced into the 2 required .dat files, and store the numbers into a marco variable each

then step 3 is to print the log file with the 3 required numbers using values retrieved from the macro variables
0
 

Author Comment

by:labradorchik
ID: 38860337
Thank you lowaloysius for your comments and suggestions!
I already defined FILE statement before the DATA step, so that part is working fine.

Is there a way to initialize and outputting all these three counts in one DATA step?
0
 

Author Comment

by:labradorchik
ID: 38861013
I am really trying to initiate and output all three counts in one DATA step, is it possible?
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 14

Accepted Solution

by:
Aloysius Low earned 500 total points
ID: 38862045
i didn't expect the output to work, but if you said so, then have u tried modifying your code such that it looks like

  IF SUBSTR(Var1,1,1) = '1'  then do;
    File "h:\xxx\test1.txt";
    writtencnt1+1;
  end;
   else IF SUBSTR(Var1,1,1) = '2' THEN do;
      File "h:\xxx\test2.txt";
      writtencnt2+1;
    end;

and see if it works?
0
 

Author Comment

by:labradorchik
ID: 38869894
Thank you, lowaloysius!
So far I am not getting anything in the counts for writtencnt1 and writtencnt2. Well, I am getting zeros for those both counts which probably means that counts don't work properly.
Could you please check below code and advice what am I doing wrong? May be there is a way to simplify this code somehow?

DATA _NULL_;
 drop readcnt writtencnt1 writtencnt2; 
 infile "allrecords" end=lastrec;
   length readcnt  8 writtencnt1 8 writtencnt2 8;
   retain readcnt  0 writtencnt1 0 writtencnt2 0;
  input @1 Var1 $char20.;

  IF SUBSTR(Var1,1,1) = '1'  then do;
    File "h:\xxx\test1.txt";
    writtencnt1+1;
  end;
   else IF SUBSTR(Var1,1,1) = '2' THEN do;
      File "h:\xxx\test2.txt";
      writtencnt2+1;
    end;

  readcnt+1;
   PUT _INFILE_;

if lastrec then do;
          file mylog.log; 
         
          /***************************
          * Records read from allrecords.dat
          ***************************/
          put "Total records read " readcnt;
         
          /********************************
          * Records written to test1.dat
          ********************************/
          put "Total records written " writtencnt1;
         
          /********************************
          * Records written to test2.dat
          ********************************/
          put "Total records written " writtencnt2;
         
         end;
RUN;

Open in new window

0
 

Author Comment

by:labradorchik
ID: 38876009
lowaloysius, after playing with a different testing data I run the code your way again and it worked fine!
As always, thank you very much!!
0
 
LVL 14

Expert Comment

by:Aloysius Low
ID: 38876118
Good to know that.. sorry been bz so haven't had a chance to look deeper but glad it's working :)
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question