BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
M_K
Calcite | Level 5 M_K
Calcite | Level 5

Hi everyone,

If i have data in ASCII format in multiple cards, how do i define it in SAS program? I want to save the data in SAS format so that once defined it can be used in future. I tried input statement. but if i am not wrong, it can be used when we have data in single card. Can i use the statement for multiple card data? How?

Regards,

MK

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

The input statement can read raw data from mulitple lines very easily.

You can use the line pointer syntax (#1,#2,#3,...) or the goto to next line pointer movement syntax (/).

If you data is not fixed columns on a fixed number of lines then you should post an example as Art suggested and others might be able to suggest how to read it.

View solution in original post

9 REPLIES 9
art297
Opal | Level 21

Can you provide an example of your data and the SAS file you want to end up with?

Tom
Super User Tom
Super User

The input statement can read raw data from mulitple lines very easily.

You can use the line pointer syntax (#1,#2,#3,...) or the goto to next line pointer movement syntax (/).

If you data is not fixed columns on a fixed number of lines then you should post an example as Art suggested and others might be able to suggest how to read it.

M_K
Calcite | Level 5 M_K
Calcite | Level 5

I am attaching the sample file.

here is the data location:

column 1card no.
column 2-3obs no.
data location:
card 1column 5v1
card 1column 7v2
card 2column 6v3
card 2 column 10-11v4

Basically the data should look like the below after writing the program:

Obs No.v1v2v3v4
115612
233823
353612
475845
595923
693734
7758.
853767
935878
1013589
M_K
Calcite | Level 5 M_K
Calcite | Level 5

I did try the suggestion made by Tom and it helped me. Thanks Tom! But can anyone suggest how to deal with it in case i dont have fixed number of lines?

M_K
Calcite | Level 5 M_K
Calcite | Level 5

Sorry for multiple posts.

for the above example that i have attached, i used the below program:

input obs 2-3

#1 v1  5

v2  7

#2 v3  6

v4  10-11;

It worked, but what if i just want to define v1 and v2  only. I am asking this because in a bigger data set i have many lines per observation and may want to define only a few for analysis. Is there any way i can use the card no. also?

Ksharp
Super User
data want(drop=a _a);
length a _a $ 1;
retain _a;
input;
id=scan(_infile_,1); a=substr(id,length(id));
if a ne _a then do;
                                  v1=scan(_infile_,2);
                                  v2=scan(_infile_,3);
                                  output;
                                   end;
_a=a;
datalines;
101 1 5
201  6   12
102 3 3
202  8   23
103 5 3
203  6   12
104 7 5
204  8   45
105 9 5
205  9   23
106 9 3
206  7   34
107 7 5
207  8   
108 5 3
208  7   67
109 3 5
209  8   78
110 1 3
210  5   89
;
run;



Ksharp

Tom
Super User Tom
Super User

Here is code that reads your sample data file.  I added a check to make sure the two lines are in the right order and are for the same observation.

data want ;

  infile 'sample data.txt' truncover ;

  input #1 card1 1 obs1 2-3 v1 5 v2 7

        #2 card2 1 obs2 2-3 v3 6 v4 10-11

  ;

  if card1 ne 1 or card2 ne 2 or obs1 ne obs2 then do;

    put 'ERROR: Cards out of order.';

    error ;

  end;

run;

One way to deal with messier data is to use conditional logic. You can read part of the line and then depending on what you see read the rest of the line differently.  Sometimes it also helps to output more than one dataset at a time.

data card1(keep=obs v1 v2)

     card2(keep=obs v3 v4)

;

  infile 'sample file.txt' truncover ;

  input card 1 @ ;

  if card=1 then do;

    input obs 2-3 v1 5 v2 7;

    output card1;

  end;

  else if card=2 then do;

    input obs 2-3 v3 6 v4 10-11 ;

    output card2;

  end;

  else do;

    put 'ERROR: Invalid card type.';

    error ;

  end;

run;

data want ;

  merge card1 card2;

  by obs ;

run;

Ksharp
Super User

Tom.

But there is a potential problem. If an obs is consisted of more than ten lines, your code will give the wrong answer.

101 4 5

201 4 6

..............

1001 3 5

1101 4 9

Ksharp

Tom
Super User Tom
Super User

You will obviously need to write code that works for the data that you are reading.  The code I wrote was for the fixed column example provided in this case. Your example seems to be of a space delimited data file.  That is also very easy to read with SAS.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 9 replies
  • 1032 views
  • 0 likes
  • 4 in conversation