BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
SAS-questioner
Obsidian | Level 7

I have a data format like below:

id    time1 time2  time3   
1       1                      
              2                
                    3          
2       1                      
              2                
                    3         

The time was on diagonal for each people, but I want to convert it to a long format with one time variable and different times under it:

ID  Time
1     1
1     2
1     3
2     1
2     2
3     3

Does anyone know how to do it? Thank you!

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data have;
input id    time1 time2  time3  ;
cards; 
1       1       .       .        
.        .      2      .          
.        .      .      3          
2       1        .     .         
.        .      2       .         
.        .       .     3 
;
data want;
 set have(rename=(id=_id));
 retain id .;
 if not missing(_id) then id=_id;
 time=coalesce(of time1-time3);
 keep id time;
run;

View solution in original post

3 REPLIES 3
ballardw
Super User

I would try:

 

data want;
   set have;
   time = max(time1,time2,time3);
   drop time1-time3;
run;
mkeintz
PROC Star

Do your second and third rows really have blank ID values?  

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Ksharp
Super User
data have;
input id    time1 time2  time3  ;
cards; 
1       1       .       .        
.        .      2      .          
.        .      .      3          
2       1        .     .         
.        .      2       .         
.        .       .     3 
;
data want;
 set have(rename=(id=_id));
 retain id .;
 if not missing(_id) then id=_id;
 time=coalesce(of time1-time3);
 keep id time;
run;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 455 views
  • 1 like
  • 4 in conversation