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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

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

View all other training opportunities.

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