BookmarkSubscribeRSS Feed
sas_
Fluorite | Level 6
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
3 REPLIES 3
SPR
Quartz | Level 8 SPR
Quartz | Level 8
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
art297
Opal | Level 21
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
Ksharp
Super User
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

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 3 replies
  • 945 views
  • 0 likes
  • 4 in conversation