Reading variables from a external file (csv) that have different number of observations

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 14
Accepted Solution

Reading variables from a external file (csv) that have different number of observations


Suppose the following csv file (named "raw"), with 3 variables (A, B, C):

A  B  C

2  5  6

4  7  5

5  0  2

6  2  4

.  9  3

.  8  5

.  5  9

.  .   7

I want to read this information and save on a data set named "raw_data_readed":

data raw_data_readed;

  infile ' c:\raw.csv ' dlm= ',' ;

  input var1 var2 var3;

run;

But when SAS reads, the result is:

obs   var1 var2 var3

1       2     5     6

2       4     7     5

3       5     0     2

4       6     2     4

Is there a way to have obs 1 to 8?

Although here I told that there are only 3 variables, in real there are more variables.

Attachment

Accepted Solutions
Solution
‎08-21-2013 07:21 PM
Super User
Posts: 11,343

Re: Reading variables from a external file (csv) that have different number of observations

Posted in reply to Wagner_Alvarenga

The DSD option is what you were looking for. Here is an example reading datalines instead of an external file so folks can see whats going on.

data raw_data_readed;

  infile datalines  dlm= ',' dsd ;

  input var1 var2 var3 var4;

datalines;

2,5,6,2

4,7,5,9

5,0,2,8

6,2,4,5

,9,3,

,8,5,7

,5,9,0

,,7,

run;

View solution in original post


All Replies
Super User
Posts: 11,343

Re: Reading variables from a external file (csv) that have different number of observations

Posted in reply to Wagner_Alvarenga

Your example data is not in CSV format. If the data is actually csv and no informat has been associated with variables I would expect to get missing values for Var1 for obs 5-8 and missing for var2 on obs 8 but not completely missing records.

You may need to post your actual reading code if not a few lines of data.

Occasional Contributor
Posts: 14

Re: Reading variables from a external file (csv) that have different number of observations

Ballardw,

I sent a file.

I would appreciate if you may help me!

Occasional Contributor
Posts: 14

Re: Reading variables from a external file (csv) that have different number of observations

Posted in reply to Wagner_Alvarenga

For the file that I sent, how should I do tho have the following variables:

Data Set TEST (with 4 variables var1, var2, var3, var4)

      

2562
4759
5028
6245
.93.
.857
.590
..7.
Solution
‎08-21-2013 07:21 PM
Super User
Posts: 11,343

Re: Reading variables from a external file (csv) that have different number of observations

Posted in reply to Wagner_Alvarenga

The DSD option is what you were looking for. Here is an example reading datalines instead of an external file so folks can see whats going on.

data raw_data_readed;

  infile datalines  dlm= ',' dsd ;

  input var1 var2 var3 var4;

datalines;

2,5,6,2

4,7,5,9

5,0,2,8

6,2,4,5

,9,3,

,8,5,7

,5,9,0

,,7,

run;

Occasional Contributor
Posts: 14

Re: Reading variables from a external file (csv) that have different number of observations

Thanks Ballardw!!

🔒 This topic is solved and locked.

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

Discussion stats
  • 5 replies
  • 193 views
  • 0 likes
  • 2 in conversation