Hi Community,
I want to split the below character variable (Have) to multiple variable (Time, Lane and Direction).
| Have | Time | Lane | Direction |
| 00:00 Lane 1 (North) | 00:00 | Lane 1 | North |
| Lane 2 (North) | Lane 2 | North | |
| Lane 3 (West) | Lane 3 | West | |
| Lane 4 (East) | Lane 4 | East | |
| All Lanes | All Lanes | ||
| 01:00 Lane 1 (North) | 01:00 | Lane 1 | North |
Also, i have dataset like below in the first two rows of the SAS dataset. Now, i want to use this and create variable. Is there any way that it automatically takes the start date alone and create a variable?
Have:
SAS dataset
Want:
I want to create variable called date from the start date mentioned in the description. Also, how to get the variable name from the row 9 ? Is there any way without using rename option?
Date
10/09/19
10/09/19
10/09/19
10/09/19
10/09/19
First, I don't understand question 2, and I don't understand where the data comes from. Could you please explain what this screen capture is that you show? Also, please explain if you can get it into a SAS data set, or is it in Excel, or something else?
For question 1, try this:
data have;
infile cards missover;
input str & :$48.;
cards;
00:00 Lane 1 (North)
Lane 2 (North)
Lane 3 (West)
Lane 4 (East)
All Lanes
01:00 Lane 1 (North)
;
data want;
set have;
index=find(str,'lane','i');
if index=1 or index=5 then do;
time=.;
lane=catx(' ',scan(str,1),scan(str,2));
direction=scan(str,3);
end;
else do;
time=input(scan(str,1,' '),time5.);
lane=catx(' ',scan(str,2),scan(str,3));
direction=scan(str,4);
end;
format time time.;
drop index;
run;
data have;
infile cards missover;
input str & :$48.;
cards;
00:00 Lane 1 (North)
Lane 2 (North)
Lane 3 (West)
Lane 4 (East)
All Lanes
01:00 Lane 1 (North)
;
data want;
set have;
pid=prxparse('/(\d\d:\d\d)?([^\(\)]+)(\(\w+\))?/o');
if prxmatch(pid,str) then do;
a=prxposn(pid,1,str);
b=prxposn(pid,2,str);
c=prxposn(pid,3,str);
end;
drop pid;
run;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.