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

Hi,

I have a case where a variable in my file looks like below:

VAR

--------

1234|2345|2222

1233|1111

11111|5555555|4444|6666|

I need to make this values look like:

var1           var2          Var3       Var4

-----           -------         --------      --------

1234       2345          2222

1233       1111            

11111      5555555    4444        6666

Please help.

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

Numerous ways to do what you want.  e.g.:

data want (drop=_: i);

  set have (rename=(var=_var));

  array var(4);

  i=1;

  do until (scan(_var,i,"|") eq "");

    var(i)=scan(_var,i,"|");

    i+1;

  end;

run;

View solution in original post

5 REPLIES 5
art297
Opal | Level 21

Numerous ways to do what you want.  e.g.:

data want (drop=_: i);

  set have (rename=(var=_var));

  array var(4);

  i=1;

  do until (scan(_var,i,"|") eq "");

    var(i)=scan(_var,i,"|");

    i+1;

  end;

run;

ren2010
Obsidian | Level 7

Thank You!

Ksharp
Super User

Art's code need to pre-define the number of variables, But if you do not know what the number of variables should be ?

data temp;
input var : $2000.;
cards;
1234|2345|2222
1233|1111
11111|5555555|4444|6666|
;
run;
data _null_;
 set temp end=last;
 if _n_ eq 1 then call execute('data want;');
 i=1;  _var=scan(var,i,'|'); 
 do while(not missing(_var));
  call execute(cats('var',i,'="',_var,'";') );
  i+1; _var=scan(var,i,'|'); 
 end;
 call execute('output;call missing(of _all_); ');
 if last then call execute('run;');
run;
 
  

Ksharp

hellind
Quartz | Level 8

ksharp, What if the dataset has a primary key? 

 

PK,VAR

1,1234|2345|2222

2,1233|1111

3,11111|5555555|4444|6666|

 

I need to make this values look like:

 

pk              var1           var2          Var3       Var4

-----           -----           -------         --------      --------

1                1234       2345          2222

2                1233       1111            

3                11111      5555555    4444        6666

Ksharp
Super User
Plz don't bring it up, it is almost five year old.
Start a brand new session.



data temp;
infile cards dlm=',';
input PK var : $2000.;
cards;
1,1234|2345|2222
2,1233|1111
3,11111|5555555|4444|6666|
;
run;
proc sql noprint;
select max(countw(var,'|')) into : n from temp;
quit;
data want;
 set temp;
 array var_{&n} $ 40;
 do i=1 to countw(var,'|');
  var_{i}=scan(var,i,'|');
 end;
 drop i ;
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
  • 16777 views
  • 1 like
  • 4 in conversation