Help using Base SAS procedures

Reg-Moving to other based on -

Reply
Contributor
Posts: 66

Reg-Moving to other based on -

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
Super User
Posts: 10,046

Re: Reg-Moving to other based on -

Hi.
What is your logic? How to split this long string ?
What is the rule.

Ksharp
Super Contributor
Super Contributor
Posts: 365

Re: Reg-Moving to other based on -

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
Super User
Posts: 10,046

Re: Reg-Moving to other based on -

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
Super User
Posts: 10,046

Re: Reg-Moving to other based on -

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
Ask a Question
Discussion stats
  • 4 replies
  • 150 views
  • 0 likes
  • 3 in conversation