BookmarkSubscribeRSS Feed
Jannet
Obsidian | Level 7

Hi Community, 

I want to split the below character variable (Have) to multiple variable (Time, Lane and Direction).

 

Have TimeLaneDirection
 00:00  Lane 1 (North)00:00Lane 1North
Lane 2 (North) Lane 2North
Lane 3 (West) Lane 3West
Lane 4 (East) Lane 4East
All Lanes
 All Lanes 
01:00  Lane 1 (North)
01:00 Lane 1North

 

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 datasetSAS 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

 

@Kurt_Bremser

 

2 REPLIES 2
PaigeMiller
Diamond | Level 26

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;

 

--
Paige Miller
Ksharp
Super User
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;
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
  • 2 replies
  • 1772 views
  • 0 likes
  • 3 in conversation