BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ybz12003
Rhodochrosite | Level 12

Hi Experts,

 

I have a sample dataset 'datain1',  it contains a column 'Name' mixed with some various text separated by comma ' , '.  

I would like to distribute the 'Name' text into different columns, result1-result3, based on the comma ' , '.    Please check the result I am looking for is 'Dataout1'

Please let me know how to approach it.   One thing I would like to point out that the 'Name' text includes different mixed names, sometime only have one name, sometime have three names, sometime have multiple names.   Therefore, the 'result' columns should be based on the largest 'Name' numbers in the 'Name' text.  The result3 column in the dataset is only the sample dataset I created.  

Thanks.

 

data datain1;
      infile datalines delimiter='/';
  input Name : $300.  ;
datalines;
	ACUTE OTITIS MEDIA,ASTIGMATISM, CONSTIPATION/
	ACUTE URI, GERD/
	BILATERAL. HYDRONEPHROSIS/
;
run;


data dataout1;
      infile datalines delimiter='/';
  input Name : $300.  Result1 : $100. Result2 : $100. Result3 : $100.;
datalines;
	ACUTE OTITIS MEDIA,ASTIGMATISM, CONSTIPATION/ACUTE OTITIS MEDIA/ASTIGMATISM/CONSTIPATION
	ACUTE URI, GERD/ACUTE URI/GERD/ /
	BILATERAL. HYDRONEPHROSIS/BILATERAL. HYDRONEPHROSIS/ / /
;
run;
1 ACCEPTED SOLUTION

Accepted Solutions
smantha
Lapis Lazuli | Level 10
data datain1;
      infile datalines delimiter='/';
  input Name : $300.  ;
  length String1 String2 String3 $300;
  array _string{*} String1 String2 String3;
  end=countc(Name,',')+1;
  do i = 1 to end;
     _String[i] = scan(strip(Name),i,',');
	 
  end;
datalines;
	ACUTE OTITIS MEDIA,ASTIGMATISM, CONSTIPATION/
	ACUTE URI, GERD/
	BILATERAL. HYDRONEPHROSIS/
;;;
run;

View solution in original post

3 REPLIES 3
smantha
Lapis Lazuli | Level 10
data datain1;
      infile datalines delimiter='/';
  input Name : $300.  ;
  length String1 String2 String3 $300;
  array _string{*} String1 String2 String3;
  end=countc(Name,',')+1;
  do i = 1 to end;
     _String[i] = scan(strip(Name),i,',');
	 
  end;
datalines;
	ACUTE OTITIS MEDIA,ASTIGMATISM, CONSTIPATION/
	ACUTE URI, GERD/
	BILATERAL. HYDRONEPHROSIS/
;;;
run;
ybz12003
Rhodochrosite | Level 12
Awesome, thanks.
ybz12003
Rhodochrosite | Level 12

One more thing, how to change the 'end' number to the largest string number. 

length String1 - String(end) $300;
	array _string{*} String1 - String(end);
	  end=countc(name,',')+1;
	  do i = 1 to end;
	     _String[i] = scan(strip(name),i,',');
  	end;

I tried, and it didn't work.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 781 views
  • 0 likes
  • 2 in conversation