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
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.
Can you provide an example of your data and the SAS file you want to end up with?
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.
I am attaching the sample file.
here is the data location:
column 1 | card no. | |
column 2-3 | obs no. | |
data location: | ||
card 1 | column 5 | v1 |
card 1 | column 7 | v2 |
card 2 | column 6 | v3 |
card 2 | column 10-11 | v4 |
Basically the data should look like the below after writing the program:
Obs No. | v1 | v2 | v3 | v4 |
1 | 1 | 5 | 6 | 12 |
2 | 3 | 3 | 8 | 23 |
3 | 5 | 3 | 6 | 12 |
4 | 7 | 5 | 8 | 45 |
5 | 9 | 5 | 9 | 23 |
6 | 9 | 3 | 7 | 34 |
7 | 7 | 5 | 8 | . |
8 | 5 | 3 | 7 | 67 |
9 | 3 | 5 | 8 | 78 |
10 | 1 | 3 | 5 | 89 |
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?
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?
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
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;
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
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.
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.
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.