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

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 1260 views
  • 0 likes
  • 3 in conversation