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

Hello everybody,

I have a string variable with varying lengths and values go like this - 

Var1

1

-1-3

456

-6-7-8

What I am trying to achieve right here is to extract the number along with their signs and create a new variable for each integer. 

Here is the layout I am hoping to look at:

var1   var2    var 3

 1                      

-1        -3            

4          5          6

-6        -7        -8

Any help with this would be greatly appreciated. 

 

Thank you

 

1 ACCEPTED SOLUTION

Accepted Solutions
SASKiwi
Opal | Level 21

There are probably more elegant ways to do this but this seems to work:

data have;
  input string $;
  cards; 
1
-1-3
456
-6-7-8
;
run;

data want;
  keep string var1 - var3;
  set have;
  array vars var1 - var3;
  start = 1;
  do i = 1 to 3;
    pos = anydigit(string, start);
    if pos then do;
      if pos = start then vars(i) = input(substr(string, start,1), 1.); 
      else vars(i) = input(substr(string, start, 2), 2.);
      start = pos + 1;
    end;
  end;
run;

View solution in original post

5 REPLIES 5
sustagens
Pyrite | Level 9
are they all single digits?
Surendra1991
Calcite | Level 5
Yes, they are all single digits
SASKiwi
Opal | Level 21

There are probably more elegant ways to do this but this seems to work:

data have;
  input string $;
  cards; 
1
-1-3
456
-6-7-8
;
run;

data want;
  keep string var1 - var3;
  set have;
  array vars var1 - var3;
  start = 1;
  do i = 1 to 3;
    pos = anydigit(string, start);
    if pos then do;
      if pos = start then vars(i) = input(substr(string, start,1), 1.); 
      else vars(i) = input(substr(string, start, 2), 2.);
      start = pos + 1;
    end;
  end;
run;
Surendra1991
Calcite | Level 5

Wonderful! The code worked like a miracle. Thank you very much!

Ksharp
Super User
data have;
  input string $;
  cards; 
1
-1-3
456
-6-7-8
;
run;
data temp;
 set have;
 id+1;
 temp=prxchange('s/(-?\d)/ \1 /',-1,string);
 do i=1 to countw(temp,' ');
  value=scan(temp,i,' ');output;
 end;
 keep id value;
run;
proc transpose data=temp out=want;
by id;
var value;
run;

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 375 views
  • 2 likes
  • 4 in conversation