DATA Step, Macro, Functions and more

i want to extract data which is in between both -xxxx- in the variable

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 115
Accepted Solution

i want to extract data which is in between both -xxxx- in the variable

I have a variable and i want to extract data like example

 

var1

kjjhf-0fdf-dfed

asd-fdfk-dfcsf

 

I want to get data which is between the -

var1

0fdf

fdfk


Accepted Solutions
Solution
‎03-21-2016 10:18 AM
Super User
Super User
Posts: 7,392

Re: i want to extract data which is in between both -xxxx- in the variable

It is a good idea to post your test data in the form of a datastep:

data have;
  var1="kjd-fhf-asd"; output;
  var1="dfsf-dfg-dwe"; output;
run;
data want;
  set have;
  want=catx(' ',substr(scan(var1,2,"-"),1,2),substr(scan(var1,2,"-"),3,1));
run;

View solution in original post


All Replies
Super User
Posts: 17,743

Re: i want to extract data which is in between both -xxxx- in the variable

SCAN() function

 

Scan(var, 2, '-');
Frequent Contributor
Posts: 115

Re: i want to extract data which is in between both -xxxx- in the variable

sorry, i gave a incomplete question. i also want space after scanning like

var1

kjd-fhf-asd

dfsf-dfg-dwe

 

i want like this

var1

fh f

df g

 

 

Super User
Posts: 17,743

Re: i want to extract data which is in between both -xxxx- in the variable

Use substr() to extract the two parts and the CAT function to create new variable. 

Solution
‎03-21-2016 10:18 AM
Super User
Super User
Posts: 7,392

Re: i want to extract data which is in between both -xxxx- in the variable

It is a good idea to post your test data in the form of a datastep:

data have;
  var1="kjd-fhf-asd"; output;
  var1="dfsf-dfg-dwe"; output;
run;
data want;
  set have;
  want=catx(' ',substr(scan(var1,2,"-"),1,2),substr(scan(var1,2,"-"),3,1));
run;
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 260 views
  • 0 likes
  • 3 in conversation