Solved

Where to place counts in SAS code?

Posted on 2013-02-06
8
619 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get MySQL database support online, now!

At Percona’s web store you can order your MySQL database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card.

 
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

Get MySQL database support online, now!

At Percona’s web store you can order your MySQL database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card.

Question has a verified solution.

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

Recently, Microsoft released a best-practice guide for securing Active Directory. It's a whopping 300+ pages long. Those of us tasked with securing our company’s databases and systems would, ideally, have time to devote to learning the ins and outs…
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Six Sigma Control Plans

627 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