DATA Step, Macro, Functions and more

read in variable number of columns

Reply
Frequent Contributor
Posts: 77

read in variable number of columns

Input data is like this:

David, 10, 3, 20
Mary, 31, 2
....

Output SAS dataset is like this:

Name Score
David 10
David 3
David 20
Mary 31
Mary 2
.....
Regular Contributor
Posts: 241

Re: read in variable number of columns

Here is one way. It is also easy to combine these two data steps into one (not shown).



   /* read the test data */


   %let n = 3; %*-- maximum number of scores --*;


   data people;


     infile cards dsd dlm="," missover;


     input name :$8. @;


     array v v1-v&n;


     do over v; 


       input v @;


     end;


     input/* release the input hold */


   cards;


   David, 10, 3, 20


   Mary, 31, 2


   Jason, 1


   ;


   run;


 


   data scores;


     set people;


     array v v1-v3;


     do over v;


       if missing(v) then continue;


       score = v;


       output


     end;


     keep name score;


   run;


 


   /* check */


   proc print data=scores noobs;


   run;


   /* on lst


   name     score


   David      10


   David       3


   David      20


   Mary       31


   Mary        2


   Jason       1


   */

Frequent Contributor
Posts: 77

Re: read in variable number of columns

Posted in reply to chang_y_chung_hotmail_com
You are the best! Smiley Happy
Super User
Posts: 10,041

Re: read in variable number of columns

Hi.
If you have understand '@' and '@@' totally.
then code will be more simply!

[pre]
data people;
infile datalines delimiter=',' truncover;
input name $ score @;
do until ( missing(score) );
output;
input score @;
end;
input; /* release the input hold */
datalines;
David,10,3,20
Mary,31,2
Jason,1
;
run;
proc print noobs;run;
[/pre]



Ksharp
Ask a Question
Discussion stats
  • 3 replies
  • 166 views
  • 0 likes
  • 3 in conversation