BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
cactooos
Obsidian | Level 7

Hi, Could you please help me to rearrange the build of this table? It can have from 0 rows to anything but hardly this will be the case of more than 20. I am already using macro variable &noobs_have elsewhere. I want the table to be like on the pic below

 

data have;
  input day : $9. Date : $16. delta;
  cards;
22OCT2020 22OCT20:15:19:10 0
22OCT2020 22OCT20:15:25:20 370
23OCT2020 23OCT20:00:19:54 2
23OCT2020 23OCT20:00:20:02 8
  ;
run;

data _NULL_;
 if 0 then set have nobs=n;
 call symputx('noobs_have',n);
 stop;
run;

 

 

Capture.PNG

 

Thank you in advance for any help

 

Cheers

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

Another solution is to assign an ID number to the pairs, and then use PROC TRANSPOSE as I showed with BY ID.

 

 

data have;
  input day :date9. Date :datetime16. delta;
  id = floor((_n_-1)/2);
  format day date9. date datetime16.;
  cards;
...
  ;

 

 

--
Paige Miller

View solution in original post

6 REPLIES 6
PaigeMiller
Diamond | Level 26

Transposing the data is a way to do this, and then you don't need to know how many rows there are, because PROC TRANSPOSE takes care of that for you. This solution assumes there are never more than two rows on a given day.

 

data have;
  input day :date9. Date :datetime16. delta;
  format day date9. date datetime16.;
  cards;
22OCT2020 22OCT20:15:19:10 0
22OCT2020 22OCT20:15:25:20 370
23OCT2020 23OCT20:00:19:54 2
23OCT2020 23OCT20:00:20:02 8
  ;
run;
proc transpose data=have out=have_t;
	by day;
	var date;
run;
data want;
	set have_t;
	delta=col2-col1;
run;
--
Paige Miller
cactooos
Obsidian | Level 7
Thank you for that, but that is the point that it can be any even number of rows in this table and all of them could be for one day as well.
PaigeMiller
Diamond | Level 26

@cactooos wrote:
Thank you for that, but that is the point that it can be any even number of rows in this table and all of them could be for one day as well.

This was not mentioned in the original message. If they are always in pairs, then the solution from @Ksharp should work well.

--
Paige Miller
Ksharp
Super User
data have;
  input day : $9. Date : $16. delta;
  cards;
22OCT2020 22OCT20:15:19:10 0
22OCT2020 22OCT20:15:25:20 370
23OCT2020 23OCT20:00:19:54 2
23OCT2020 23OCT20:00:20:02 8
  ;
run;

data want;
 do until(last.day);
  set have;
  by day;
  if first.day then from=date;
 end;
 to=date;
 keep day from to delta;
 run;
cactooos
Obsidian | Level 7

Thanks, I didn't mention that in the first message. They always comes as a pair but can be any number of pairs per table and per day. So this is also possible:

data have;
  input day : $9. Date : $16. delta;
  cards;
22OCT2020 22OCT20:15:19:10 0
22OCT2020 22OCT20:15:25:20 370
22OCT2020 22OCT20:16:19:10 0
22OCT2020 22OCT20:16:25:30 380
22OCT2020 22OCT20:17:19:10 0
22OCT2020 22OCT20:17:25:40 390
24OCT2020 24OCT20:00:19:54 2
24OCT2020 24OCT20:00:20:02 8
  ;
run;
PaigeMiller
Diamond | Level 26

Another solution is to assign an ID number to the pairs, and then use PROC TRANSPOSE as I showed with BY ID.

 

 

data have;
  input day :date9. Date :datetime16. delta;
  id = floor((_n_-1)/2);
  format day date9. date datetime16.;
  cards;
...
  ;

 

 

--
Paige Miller

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 6 replies
  • 899 views
  • 4 likes
  • 3 in conversation