Help using Base SAS procedures

Reg Separation

Reply
Contributor
Posts: 66

Reg Separation

Hi i am having dataset wrk in that i am having two variables id and main_obs
for ex
1 typist,-type,printing,computer-coding,writing, -nowork,driver-driving,caring,ceo-mainhead,cordinator,work-,asstgm-lesswrk,maintanace

like this line is 300 in length i wnat out put like this as the - can be used as a delimiter form the last comma(,) and hifen(-)

output:


id Main_obs:
1 typist-type,printing
1 computer-coding,writing
1 -nowork
1 driver-driving,caring
1 ceo-mainhead,cordinator
1 work-
1 asstgm-lesswrk,maintanace
Super Contributor
Super Contributor
Posts: 365

Re: Reg Separation

Hello SAS_,

This is a possible solution:
[pre]
data i;
input id s $ 3-118;
datalines;
1 typist-type,printing,computer-coding,writing,driver-driving,caring,ceo-mainhead,cordinator,asstgm-lesswrk,maintanace
run;
data t;
length r $50;
set i;
if FIRST.id then do; i=1; r="1"; end;
do until (r="");
r=SCAN(s,i,",");
i+1;
output;
end;
by id;
run;
data r (keep=id Main_obs);
retain id Main_obs;
set t;
if MOD(_n_,2)=1 then Main_obs=r;
else do;
Main_obs=TRIM(MAIN_Obs)||","||r;
output;
end;
by id;
run;
[/pre]
Sincerely,
SPR
PROC Star
Posts: 7,467

Re: Reg Separation

Similar to SPR's suggestion, but all in one datastep:
[pre]
data want (keep=id Main_obs);
informat Main_obs_in $300.;
length Main_obs $30;
input id Main_obs_in;
i=1;
do while (scan(Main_obs_in,i,',') ne "");
Main_obs=catx(",",scan(Main_obs_in,i,','),scan(Main_obs_in,i+1,','));
i+2;
output;
end;
cards;
1 typist-type,printing,computer-coding,writing,driver-driving,caring,ceo-mainhead,cordinator,asstgm-lesswrk,maintanace
;
run;
[/pre]
HTH,
Art
Super User
Posts: 10,020

Re: Reg Separation

Similar to Art.T's suggestion, but all in one datastep:
[pre]
data want(keep=id main_obs where=(main_obs is not missing)) ;
infile datalines delimiter=', ' truncover;
input id @;
informat a b $20.;
do until(missing(a) or missing(b));
input a b @;
main_obs=catx(',',a,b); output;
end;
input;
datalines;
1 typist-type,printing,computer-coding,writing,driver-driving,caring,ceo-mainhead,cordinator,asstgm-lesswrk,maintanace
2 typist-type,printing,computer-coding,writing,driver-driving,caring,ceo-mainhead,cordinator,asstgm-lesswrk,maintanace
;
run;
[/pre]


Ksharp Message was edited by: Ksharp
Ask a Question
Discussion stats
  • 3 replies
  • 130 views
  • 0 likes
  • 4 in conversation