Solved

Where to place counts in SAS code?

Posted on 2013-02-06
8
570 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
A short article about problems I had with the new location API and permissions in Marshmallow
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now