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 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 17775 views
  • 1 like
  • 4 in conversation