BookmarkSubscribeRSS Feed
MarcTC
Obsidian | Level 7
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
.....
3 REPLIES 3
chang_y_chung_hotmail_com
Obsidian | Level 7
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


   */

MarcTC
Obsidian | Level 7
You are the best! 🙂
Ksharp
Super User
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

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1393 views
  • 0 likes
  • 3 in conversation