Solved

How to split records in .dat file in SAS?

Posted on 2013-02-04
6
564 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 11

Accepted Solution

by:
theartfuldazzler earned 500 total points
Comment Utility
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
Comment Utility
Great!! I will test it and let you know how it turned out.
Thank you, theartfuldazzler!
0
 

Author Comment

by:labradorchik
Comment Utility
theartfuldazzler, your second example worked fine.
This is exactly what I needed!
Thank you very much for your comments and suggestions!!
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This article explains all about SQL Server Piecemeal Restore with examples in step by step manner.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
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.

728 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

14 Experts available now in Live!

Get 1:1 Help Now