Help using Base SAS procedures

How to define variables is ASCII data???

Accepted Solution Solved
Reply
Occasional Contributor M_K
Occasional Contributor
Posts: 14
Accepted Solution

How to define variables is ASCII data???

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


Accepted Solutions
Solution
‎12-26-2011 03:24 PM
Super User
Super User
Posts: 7,039

How to define variables is ASCII data???

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


All Replies
PROC Star
Posts: 7,468

How to define variables is ASCII data???

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

Solution
‎12-26-2011 03:24 PM
Super User
Super User
Posts: 7,039

How to define variables is ASCII data???

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.

Occasional Contributor M_K
Occasional Contributor
Posts: 14

Re: How to define variables is ASCII data???

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
Attachment
Occasional Contributor M_K
Occasional Contributor
Posts: 14

How to define variables is ASCII data???

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?

Occasional Contributor M_K
Occasional Contributor
Posts: 14

How to define variables is ASCII data???

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?

Super User
Posts: 10,023

How to define variables is ASCII data???

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

Super User
Super User
Posts: 7,039

Re: How to define variables is ASCII data???

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;

Super User
Posts: 10,023

Re: How to define variables is ASCII data???

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

Super User
Super User
Posts: 7,039

Re: How to define variables is ASCII data???

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.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

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