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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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