BookmarkSubscribeRSS Feed
sas_
Fluorite | Level 6
data a;
input id s $ 3-118;
datalines;
1 prg-code,main,IT-Hardware,Ins,Main,Non_IT- ,WOrk-Hardwrk,giveup,main,doit, -Nowork,ceo-cash,main,user,repair,nonit,
run;

LOGIC :I WANT TO MOVE IT IN TO ANOHTER ROW BASED ON "- "(HI FUN) BEFORE HIFUN FIRST COMMA .

i wnat it in data set a2 as

output:

1 prg-code,main
1 IT-Hardware,Ins,Main
1 Non_IT-
1 WOrk-Hardwrk,giveup,main,doit
1 -Nowork
1 ceo-cash,main,user,repair,nonit
4 REPLIES 4
Ksharp
Super User
Hi.
What is your logic? How to split this long string ?
What is the rule.

Ksharp
SPR
Quartz | Level 8 SPR
Quartz | Level 8
Hello Sas_,

This is a solution:
[pre]
data i;
input id s $ 3-118;
datalines;
1 prg-code,main,IT-Hardware,Ins,Main,Non_IT- ,WOrk-Hardwrk,giveup,main,doit, -Nowork,ceo-cash,main,user,repair,nonit,
run;
data t;
set i;
length t $116;
do until (t = "");
i+1;
t=SCAN(s,i,",");
if SCAN(t,1,"-") NE t then j+1;
if t NE "" then output;
end;
run;
data r;
retain id r;
set t;
if FIRST.j then r=t;
else r=CATS(r,",",t);
if LAST.j then output;
by j;
keep id r;
run;
[/pre]
Sincerely,
SPR
Ksharp
Super User
OK.Be honest. It is more complicated than I imaged.
[pre]



data a(keep= id ss where=(ss is not missing));
input id s $ 3-118;
length ss $ 50;
retain ss;
i=1; _s=scan(s,1,',');
do while(not missing(_s));
if findc(_s,'-') then do;output;call missing(ss);end;
ss=catx(',',ss,_s);
i+1;
_s=scan(s,i,',');
if missing(_s) then output;
end;
call missing(ss);
datalines;
1 prg-code,main,IT-Hardware,Ins,Main,Non_IT- ,WOrk-Hardwrk,giveup,main,doit, -Nowork,ceo-cash,main,user,repair,nonit,
2 prgcode,main,IT-Hardware,Ins,Main,Non_IT- ,WOrk-Hardwrk,giveup,main,doit, -Nowork,ceo-cash,main,user,repair,non-it,
3 prg-code,main,IT-Hardware,Ins,Main,Non_IT- ,WOrk-Hardwrk,giveup,main,doit, -Nowork,ceo-cash,main,user,repair,non-it,
run;

[/pre]


Ksharp
Ksharp
Super User
Another solution.
[pre]




data a;
input id s $ 3-118;
datalines;
1 prg-code,main,IT-Hardware,Ins,Main,Non_IT- ,WOrk-Hardwrk,giveup,main,doit, -Nowork,ceo-cash,main,user,repair,nonit,
2 prgcode,main,IT-Hardware,Ins,Main,Non_IT- ,WOrk-Hardwrk,giveup,main,doit, -Nowork,ceo-cash,main,user,repair,non-it,
3 prg-code,main,IT-Hardware,Ins,Main,Non_IT- ,WOrk-Hardwrk,giveup,main,doit, -Nowork,ceo-cash,main,user,repair,non-it,
;
run;
data temp(drop=s i);
set a;
if id ne lag(id) then count+1;
i=1;ss=scan(s,i,',');
do while(not missing(ss));
if findc(ss,'-') then count+1;
output;
i+1;
ss=scan(s,i,',');
end;
run;
data want;
set temp;
by count notsorted;
length s $ 100;
retain s;
if first.count then call missing(s);
s=catx(',',s,ss);
if last.count then output;
drop ss count;
run;



[/pre]




Ksharp

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

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