Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more

x
?
Solved

How to split records in .dat file in SAS?

Posted on 2013-02-04
6
Medium Priority
?
623 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 11

Accepted Solution

by:
theartfuldazzler earned 2000 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 does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
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.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

647 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