Solved

How to split records in .dat file in SAS?

Posted on 2013-02-04
6
613 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
[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
  • 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

691 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