Solved

How to split records in .dat file in SAS?

Posted on 2013-02-04
6
598 Views
Last Modified: 2013-02-06
How may I split records when records are used from .dat file in SAS?

I have allrecords.dat file and I am trying to split this file into two separate files (records1.dat and records2.dat) with the following criteria:

write records with variable Var1(1:1)=1 to records1.dat (if first number in Var1 is 1 then this record should go to records1.dat)
write records with variable Var1(1:2)=2 to records2.dat (if first number in Var1 is 2 then this record should go to records2.dat)

Note: allrecords.dat file has about 20 different variables but I only would like to use Var1 for splitting purposes. I still need to keep all other variables with all data.

I have started with the below code:
data data1 data2;
  infile "allrecords";
   input @1 Var1 $char20. @22 Var2;
    if Var1='1*' then data1;
    if Var1='2*' then data2;
run;
  
data _null_;
   set data1;
     file records1;
       put @1   Var1 $char20.
             @22 Var2 $char.;
run;

data _null_;
   set data2; 
     file records2;
       put @1   Var1 $char20.
             @22 Var2 $char.;
run;

Open in new window


Any examples, comments or suggestions would be greatly appreciated!
0
Comment
Question by:labradorchik
  • 3
  • 3
6 Comments
 
LVL 11

Expert Comment

by:theartfuldazzler
ID: 38854652
Hi

Use the OUTPUT statement:

data data1 data2;
  infile "allrecords";
   input @1 Var1 $char20. @22 Var2;
    if substr(Var1,1,1)='1' then OUTPUT data1;
    if Substr(Var1,1,1)='2' then OUTPUT data2;
run;
0
 

Author Comment

by:labradorchik
ID: 38855067
Thank you very much theartfuldazzler!

Should I still use the code below to put all data from data1 and data2 datasets to records1.dat and records2.dat? Is this the most efficient way?

data _null_;
   set data1;
     file records1;
       put @1   Var1 $char20.
             @22 Var2 $char.;
run;

data _null_;
   set data2; 
     file records2;
       put @1   Var1 $char20.
             @22 Var2 $char.;
run;

Open in new window

0
 
LVL 11

Expert Comment

by:theartfuldazzler
ID: 38855133
Hi

you can do it in one step:


DATA _NULL_;
  infile "allrecords";
   input @1 Var1 $char20. @22 Var2;

  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";

   PUT _ALL_;
RUN;
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 11

Accepted Solution

by:
theartfuldazzler earned 500 total points
ID: 38855142
Hi

Even better - if you just want to copy the one line exactly from the one .dat to another:

DATA _NULL_;
  infile "allrecords";
   input ;

  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";

   PUT _INFILE_;
RUN;

Open in new window

0
 

Author Comment

by:labradorchik
ID: 38855290
Great!! I will test it and let you know how it turned out.
Thank you, theartfuldazzler!
0
 

Author Comment

by:labradorchik
ID: 38860124
theartfuldazzler, your second example worked fine.
This is exactly what I needed!
Thank you very much for your comments and suggestions!!
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

Suggested Solutions

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.
As technology users and professionals, we’re always learning. Our universal interest in advancing our knowledge of the trade is unmatched by most industries. It’s a curiosity that makes sense, given the climate of change. Within that, there lies a…
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
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 …

856 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