Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Where to place counts in SAS code?

Posted on 2013-02-06
8
Medium Priority
?
622 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
Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
LVL 14

Accepted Solution

by:
Aloysius Low earned 2000 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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
What we learned in Webroot's webinar on multi-vector protection.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Introduction to Processes

730 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