<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Creating new variables from a complex text variable in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Creating-new-variables-from-a-complex-text-variable/m-p/870996#M38564</link>
    <description>&lt;P&gt;You just need to replace the INPUT statement with a SET statement naming your input dataset:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data patient_icd;
  keep Patient_ID ICD_VAR;
  set have;
   do i = 1 to 13;
     ICD_VAR = scan(ICD_CODES, i, '|');
	 if ICD_VAR = '' then leave;
	 output;
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 21 Apr 2023 03:49:30 GMT</pubDate>
    <dc:creator>SASKiwi</dc:creator>
    <dc:date>2023-04-21T03:49:30Z</dc:date>
    <item>
      <title>Creating new variables from a complex text variable</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-new-variables-from-a-complex-text-variable/m-p/870895#M38559</link>
      <description>&lt;P&gt;&lt;SPAN&gt;I have a dataset that contains several ICD-10 codes along with their explanations. I would like to generate new variables based on the ICD-10 codes in the dataset. New variables will be like ICD_var1,ICD_var2,... on the table. I would like to improve my abilities for this kind of variables. I could not find similar examples. Could you please help me? Thank you in advance.(The screenshot&amp;nbsp;is what I want to have)&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="OJohn_StaT_0-1682022532239.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/82877i0ECA88E0A6BEEB32/image-size/medium?v=v2&amp;amp;px=400" role="button" title="OJohn_StaT_0-1682022532239.png" alt="OJohn_StaT_0-1682022532239.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/***My data creating code***/
data patient_icd;
   input Patient_ID ICD_CODES $100.;
   ICD_VAR1 = scan(ICD_CODES, 1, '|');
   ICD_VAR2 = scan(ICD_CODES, 2, '|');
   ICD_VAR3 = scan(ICD_CODES, 3, '|');
   ICD_VAR4 = scan(ICD_CODES, 4, '|');
   ICD_VAR5 = scan(ICD_CODES, 5, '|');
   ICD_VAR6 = scan(ICD_CODES, 6, '|');
   ICD_VAR7 = scan(ICD_CODES, 7, '|');
   ICD_VAR8 = scan(ICD_CODES, 8, '|');
   ICD_VAR9 = scan(ICD_CODES, 9, '|');
   ICD_VAR10 = scan(ICD_CODES, 10, '|');
   ICD_VAR11 = scan(ICD_CODES, 11, '|');
   ICD_VAR12 = scan(ICD_CODES, 12, '|');
   ICD_VAR13 = scan(ICD_CODES, 13, '|');
   datalines;
234423 (#1) Sickle-cell disease without crisis (D57.1) [POA=] | (#2) Idiopathic aseptic necrosis of unspecified bone (M87.00) [POA=] | (#3) Anxiety disorder, unspecified (F41.9) [POA=] | (#4) Osteonecrosis, unspecified (M87.9) [POA=] | (#5) Other symptoms and signs involving emotional state (R45.89) [POA=] | (#6) Other specified health status (Z78.9) [POA=] | (#7) Other specified counseling (Z71.89) [POA=] | (#8) Other long term (current) drug therapy (Z79.899) [POA=] | (#9) Encounter for therapeutic drug level monitoring (Z51.81) [POA=] | (#10) Counseling, unspecified (Z71.9) [POA=]
8723492 (#1) Sickle-cell disease without crisis (D57.1) [POA=] | (#2) Idiopathic aseptic necrosis of unspecified bone (M87.00) [POA=] | (#3) Osteonecrosis, unspecified (M87.9) [POA=] |&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 20 Apr 2023 20:36:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-new-variables-from-a-complex-text-variable/m-p/870895#M38559</guid>
      <dc:creator>OJohn_StaT</dc:creator>
      <dc:date>2023-04-20T20:36:39Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variables from a complex text variable</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-new-variables-from-a-complex-text-variable/m-p/870901#M38560</link>
      <description>&lt;P&gt;How exactly do you expect to use the resulting data set?&lt;/P&gt;
&lt;P&gt;For a great many purposes you would be better off making a single ICD_10 variable and one observation per value. The question is what rules can be used to identify just the ICD-10 values.&lt;/P&gt;
&lt;P&gt;At first pass you might think that looking for stuff inside () will suffice. But you have this "(current)" appear in the #8 block of the first patient.&lt;/P&gt;</description>
      <pubDate>Thu, 20 Apr 2023 21:32:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-new-variables-from-a-complex-text-variable/m-p/870901#M38560</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-04-20T21:32:50Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variables from a complex text variable</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-new-variables-from-a-complex-text-variable/m-p/870933#M38561</link>
      <description>&lt;P&gt;Thank you for your help. I am trying to see each patients different diagnosis separately as a icd_var1, icd_var2,...&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Apr 2023 22:37:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-new-variables-from-a-complex-text-variable/m-p/870933#M38561</guid>
      <dc:creator>OJohn_StaT</dc:creator>
      <dc:date>2023-04-20T22:37:57Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variables from a complex text variable</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-new-variables-from-a-complex-text-variable/m-p/870973#M38562</link>
      <description>&lt;P&gt;As&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;has already suggested, separating the diagonoses into rows rather than variables makes it easier for analysis:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data patient_icd;
  keep Patient_ID ICD_VAR;
   input @1 Patient_ID $7. @8 ICD_CODES $600.;
   do i = 1 to 13;
     ICD_VAR = scan(ICD_CODES, i, '|');
	 if ICD_VAR = '' then leave;
	 output;
   end;
   datalines;
234423 (#1) Sickle-cell disease without crisis (D57.1) [POA=] | (#2) Idiopathic aseptic necrosis of unspecified bone (M87.00) [POA=] | (#3) Anxiety disorder, unspecified (F41.9) [POA=] | (#4) Osteonecrosis, unspecified (M87.9) [POA=] | (#5) Other symptoms and signs involving emotional state (R45.89) [POA=] | (#6) Other specified health status (Z78.9) [POA=] | (#7) Other specified counseling (Z71.89) [POA=] | (#8) Other long term (current) drug therapy (Z79.899) [POA=] | (#9) Encounter for therapeutic drug level monitoring (Z51.81) [POA=] | (#10) Counseling, unspecified (Z71.9) [POA=]
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 21 Apr 2023 00:37:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-new-variables-from-a-complex-text-variable/m-p/870973#M38562</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2023-04-21T00:37:51Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variables from a complex text variable</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-new-variables-from-a-complex-text-variable/m-p/870991#M38563</link>
      <description>&lt;P&gt;Your code worked&amp;nbsp; and I wrote the following codes and I got what I wanted so for. I have a questions. How can I adjust your code for using a sas dataset instead of datalines statement for creating&amp;nbsp;&amp;nbsp;patient_icd dataset. I tried but I could not do it. Can you help me?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data patient_icd_2;&lt;BR /&gt;set patient_icd;&lt;BR /&gt;icd_codes = scan(icd_var, -2, '()');&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;proc transpose data=patient_icd_2 out=patient_icd_3;&lt;BR /&gt;by patient_id;&lt;BR /&gt;var icd_codes;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;/* Rename the variable and remove the prefix 'COL' */&lt;BR /&gt;data patient_icd_4;&lt;BR /&gt;set patient_icd_3;&lt;BR /&gt;rename col1=icd_var1 col2=icd_var2 col3=icd_var3 col4=icd_var4 col5=icd_var5 &lt;BR /&gt;col6=icd_var6 col7=icd_var7 col8=icd_var8 col9=icd_var9 col10=icd_var10;&lt;BR /&gt;drop _name_;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Fri, 21 Apr 2023 03:15:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-new-variables-from-a-complex-text-variable/m-p/870991#M38563</guid>
      <dc:creator>OJohn_StaT</dc:creator>
      <dc:date>2023-04-21T03:15:37Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variables from a complex text variable</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-new-variables-from-a-complex-text-variable/m-p/870996#M38564</link>
      <description>&lt;P&gt;You just need to replace the INPUT statement with a SET statement naming your input dataset:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data patient_icd;
  keep Patient_ID ICD_VAR;
  set have;
   do i = 1 to 13;
     ICD_VAR = scan(ICD_CODES, i, '|');
	 if ICD_VAR = '' then leave;
	 output;
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 21 Apr 2023 03:49:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-new-variables-from-a-complex-text-variable/m-p/870996#M38564</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2023-04-21T03:49:30Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variables from a complex text variable</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-new-variables-from-a-complex-text-variable/m-p/871058#M38566</link>
      <description>&lt;P&gt;If you really need a single patient_id with multiple variables for each ICD value then you can do that in a single data step:&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Set this to the maximum number of icdVar values in your icd_codes variable */
/* Note you may need to set this dynamically depending on your data           */
%let icdVarCnt=13 ;

data patient_icd;
	/* Keep the variables you want in the output dataset */
	keep patient_id icd_var1-icd_var&amp;amp;icdVarCnt ;
	/* Create an array to hold the individual icd_var values */
	array icd_var{&amp;amp;icdVarCnt} $100 ;
	/* Read test data */
	input @1 Patient_ID $7. @8 ICD_CODES $600.;
	/* Loop through the icd_vsr variable extracting individual icd values */
	do i=1 to &amp;amp;icdVarCnt ; 
		/* extract individual icd values */ 
		ICD_VAR(i) = scan(ICD_CODES, i, '|') ;
		/* Drop out off the loop if there are no more individual icd values */
		if ICD_VAR(i) = '' then leave;
	end;
	/* write observation to output dataset */
	output work.patient_icd ;
datalines;
234423 (#1) Sickle-cell disease without crisis (D57.1) [POA=] | (#2) Idiopathic aseptic necrosis of unspecified bone (M87.00) [POA=] | (#3) Anxiety disorder, unspecified (F41.9) [POA=] | (#4) Osteonecrosis, unspecified (M87.9) [POA=] | (#5) Other symptoms and signs involving emotional state (R45.89) [POA=] | (#6) Other specified health status (Z78.9) [POA=] | (#7) Other specified counseling (Z71.89) [POA=] | (#8) Other long term (current) drug therapy (Z79.899) [POA=] | (#9) Encounter for therapeutic drug level monitoring (Z51.81) [POA=] | (#10) Counseling, unspecified (Z71.9) [POA=]
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Like others who have replied, I would not recommend this approach but only you know what your intended outcome is.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 21 Apr 2023 11:00:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-new-variables-from-a-complex-text-variable/m-p/871058#M38566</guid>
      <dc:creator>AMSAS</dc:creator>
      <dc:date>2023-04-21T11:00:25Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variables from a complex text variable</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-new-variables-from-a-complex-text-variable/m-p/871070#M38568</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines truncover;
input Patient_ID ICD_CODES $600.;
datalines;
234423 (#1) Sickle-cell disease without crisis (D57.1) [POA=] | (#2) Idiopathic aseptic necrosis of unspecified bone (M87.00) [POA=] | (#3) Anxiety disorder, unspecified (F41.9) [POA=] | (#4) Osteonecrosis, unspecified (M87.9) [POA=] | (#5) Other symptoms and signs involving emotional state (R45.89) [POA=] | (#6) Other specified health status (Z78.9) [POA=] | (#7) Other specified counseling (Z71.89) [POA=] | (#8) Other long term (current) drug therapy (Z79.899) [POA=] | (#9) Encounter for therapeutic drug level monitoring (Z51.81) [POA=] | (#10) Counseling, unspecified (Z71.9) [POA=]
8723492 (#1) Sickle-cell disease without crisis (D57.1) [POA=] | (#2) Idiopathic aseptic necrosis of unspecified bone (M87.00) [POA=] | (#3) Osteonecrosis, unspecified (M87.9) [POA=] |
;
run;

data temp;
 set have;
 pid=prxparse('/\(.+?\)/');
 s=1;e=length(ICD_CODES);
 call prxnext(pid,s,e,ICD_CODES,p,l);
 do while(p&amp;gt;0);
  value=compress(substr(ICD_CODES,p,l),'()');
  if value ne: '#' then output;
  call prxnext(pid,s,e,ICD_CODES,p,l);
 end;
 keep Patient_ID ICD_CODES value;
run;
proc transpose data=temp out=want(drop=_NAME_) prefix=ICD_VAR;
by Patient_ID ICD_CODES ;
var value;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 21 Apr 2023 11:49:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-new-variables-from-a-complex-text-variable/m-p/871070#M38568</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-04-21T11:49:59Z</dc:date>
    </item>
  </channel>
</rss>

