<?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: How to replace text from one column to another? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-replace-text-from-one-column-to-another/m-p/952198#M372159</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
length ID $10. score 8. formula $200.;
infile datalines dsd missover  ;
input ID $ score formula $;
datalines;
KRI1,,KRI2/KRI3
KRI2,1,
KRI3,10,
KRI4,,(KRI5-KRI6)*100
KRI5,13,
KRI6,16,
KRI7,,(KRI8+KRI9)/(KRI10-KRI11)
KRI8,13,
KRI9,16,
KRI10,1,
KRI11,10,
;
run;
proc transpose data=have(where=(score is not missing)) out=temp(drop=_name_) prefix=_;
id id;
var score;
run;
data want;
 if _n_=1 then set temp;
 set have(where=(formula is not missing));
 array x{*} _:;
 do i=1 to dim(x);
  formula=tranwrd(upcase(formula),upcase(substr(vname(x{i}),2)),strip(x{i}) );
 end;
 score=input(resolve(catt('%sysevalf(',formula,')')),best.);
 drop i _:;
 run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sat, 30 Nov 2024 07:52:05 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2024-11-30T07:52:05Z</dc:date>
    <item>
      <title>How to replace text from one column to another?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-replace-text-from-one-column-to-another/m-p/952167#M372146</link>
      <description>&lt;P&gt;Hi , I have a table as below&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
length ID $10. score 8. formula $200.;
infile datalines dsd missover  ;
input ID $ score formula $;
datalines;
KRI1,,KRI2/KRI3
KRI2,1,
KRI3,10,
KRI4,,(KRI5-KRI6)*100
KRI5,13,
KRI6,16,
KRI7,,(KRI8+KRI9)/(KRI10-KRI11)
KRI8,13,
KRI9,16,
KRI10,1,
KRI11,10,
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I want to replace the formula ID's with actual values. i can filter out the formula data by using where the formula is not empty but my actual table has +250k rows and i can't do a standard loop and replace method.&amp;nbsp; &amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;I want my want table as&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
length ID $10. formula $200. score 8. ;
infile datalines dsd missover  ;
input ID $ formula $ score;
datalines;
KRI1,1/10,0.1
KRI4,(13-16)*100,-300
KRI7,(13+16)/(1-10),-3.22
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;for a smaller data I am using loop and tranwrd function and then use eval method to calculate the final value.&lt;BR /&gt;&lt;BR /&gt;How can i do the same for&amp;nbsp; a large dataset?&lt;/P&gt;</description>
      <pubDate>Fri, 29 Nov 2024 12:31:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-replace-text-from-one-column-to-another/m-p/952167#M372146</guid>
      <dc:creator>Azeem112</dc:creator>
      <dc:date>2024-11-29T12:31:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to replace text from one column to another?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-replace-text-from-one-column-to-another/m-p/952171#M372148</link>
      <description>&lt;P&gt;Try this. Should perform reasonably.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Feel free to ask &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp;

   format ID formula score;

   merge have
         have (firstobs = 2 keep = score rename = score = s);

   if score = . then do;
      f = formula;
      i = ID;
   end;

   if score then f = tranwrd(f, strip(ID), put(strip(score), 8.));

   if s = . then do;
      formula = compress(f);
      ID = i;
      score = input(resolve('%sysevalf('||formula||')'), best8.);
      output;
   end;

   retain f i;
   keep ID formula score;
   format score2 8.2;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;Result:&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;ID    formula         score
KRI1  1/10            0.1
KRI4  (13-16)*100     -300
KRI7  (13+16)/(1-10)  -3.22222&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 29 Nov 2024 13:31:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-replace-text-from-one-column-to-another/m-p/952171#M372148</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2024-11-29T13:31:12Z</dc:date>
    </item>
    <item>
      <title>Re: How to replace text from one column to another?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-replace-text-from-one-column-to-another/m-p/952173#M372149</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/163076"&gt;@Azeem112&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Stealing some code from&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;&amp;nbsp;response &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;, here is another approach&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
length ID $10. score 8. formula $200.;
infile datalines dsd missover  ;
input ID $ score formula $;
datalines;
KRI1,,KRI2/KRI3
KRI2,1,
KRI3,10,
KRI4,,(KRI5-KRI6)*100
KRI5,13,
KRI6,16,
KRI7,,(KRI8+KRI9)/(KRI10-KRI11)
KRI8,13,
KRI9,16,
KRI10,1,
KRI11,10,
;
run;

/* Create custom informat that can be used to convert ID to matching numeric value */
data infmt;
  retain fmtname 'ref' type 'I';
  /* Exclude formula records */
  set have(where=(missing(formula)) rename=(id=start score=label)); 
run;
proc format cntlin=work.infmt; run;

/* Use the custom Informat with Regular Expression */
data want(KEEP=ID formula score);
  set have(where=(strip(formula) ne ''));
  length formula2 $400;
  formula2 = cats('%sysevalf(',prxchange('s/KRI(\d+)/%sysfunc(inputn(KRI$1,ref.))/',-1,formula),')');
  score = input(resolve(formula2),best.);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Hope this helps&lt;/P&gt;</description>
      <pubDate>Fri, 29 Nov 2024 14:25:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-replace-text-from-one-column-to-another/m-p/952173#M372149</guid>
      <dc:creator>AhmedAl_Attar</dc:creator>
      <dc:date>2024-11-29T14:25:47Z</dc:date>
    </item>
    <item>
      <title>Re: How to replace text from one column to another?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-replace-text-from-one-column-to-another/m-p/952198#M372159</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
length ID $10. score 8. formula $200.;
infile datalines dsd missover  ;
input ID $ score formula $;
datalines;
KRI1,,KRI2/KRI3
KRI2,1,
KRI3,10,
KRI4,,(KRI5-KRI6)*100
KRI5,13,
KRI6,16,
KRI7,,(KRI8+KRI9)/(KRI10-KRI11)
KRI8,13,
KRI9,16,
KRI10,1,
KRI11,10,
;
run;
proc transpose data=have(where=(score is not missing)) out=temp(drop=_name_) prefix=_;
id id;
var score;
run;
data want;
 if _n_=1 then set temp;
 set have(where=(formula is not missing));
 array x{*} _:;
 do i=1 to dim(x);
  formula=tranwrd(upcase(formula),upcase(substr(vname(x{i}),2)),strip(x{i}) );
 end;
 score=input(resolve(catt('%sysevalf(',formula,')')),best.);
 drop i _:;
 run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 30 Nov 2024 07:52:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-replace-text-from-one-column-to-another/m-p/952198#M372159</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-11-30T07:52:05Z</dc:date>
    </item>
    <item>
      <title>Re: How to replace text from one column to another?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-replace-text-from-one-column-to-another/m-p/952214#M372167</link>
      <description>&lt;P&gt;If your data are always structured such that a formula observation is immediately followed by all of its components, then the solution offered by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;&amp;nbsp;seems to be the most straightforward.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But if the components can be anywhere in the dataset, then you need to have access to all the known score values in the dataset before you can reliably interpret every formula.&amp;nbsp;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;'s solution does that.&amp;nbsp; The only downside I see to the latter is that, for each formula, it loops through all known scores in the dataset ("do i=1 to dim(_x)").&amp;nbsp; It could be a big loop.&amp;nbsp; You said you have 250,000 obs, so maybe you have 50,000 formulas and 200,000 known scores to loop through for each formula, yes?.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's a way to have every known score available for each formula, but loop only through those scores actually in the formula:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  length ID $10. score 8. formula $200.;
  infile datalines dsd missover  ;
  input ID $ score formula $;
datalines;
KRI1,,KRI2/KRI3
KRI2,1,
KRI3,10,
KRI4,,(KRI5-KRI6)*100
KRI5,13,
KRI6,16,
KRI7,,(KRI8+KRI9)/(KRI10-KRI11)
KRI8,13,
KRI9,16,
KRI10,1,
KRI11,10,
run;

data want (drop=_: i);
  set have (where=(formula is not missing));
  if _n_=1 then do;
    declare hash sc (dataset:'have (keep=id score  where=(score is not missing))');
      sc.definekey('id');
      sc.definedata('score');
      sc.definedone();
  end;

  resolve_text=compress(formula);

  do i=1 to countw(formula,'-+*/() ');  /* For every term in the formula */
    _term=scan(formula,i,'-+*/() ');    /* Get the term identity, then insert its score */
    if sc.find(key:_term)=0 then resolve_text=tranwrd(resolve_text,trim(_term),trim(cats(score)));
  end;
  score=input(resolve(cats('%sysevalf(',resolve_text,')')),32.);
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that the sc.find(key:_term) method will retrieve the score from the SC hash, but not the ID.&amp;nbsp; That's because the ID is only in the &lt;EM&gt;&lt;STRONG&gt;.definekey&lt;/STRONG&gt;&lt;/EM&gt; method, but not in the &lt;EM&gt;&lt;STRONG&gt;.definedata&lt;/STRONG&gt;&lt;/EM&gt; method.&lt;/P&gt;</description>
      <pubDate>Sun, 01 Dec 2024 02:59:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-replace-text-from-one-column-to-another/m-p/952214#M372167</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2024-12-01T02:59:27Z</dc:date>
    </item>
    <item>
      <title>Re: How to replace text from one column to another?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-replace-text-from-one-column-to-another/m-p/952222#M372168</link>
      <description>&lt;P&gt;Are your ID values valid SAS names?&lt;/P&gt;
&lt;P&gt;Transpose your non formula values to a single observation.&amp;nbsp; First see if PROC TRANSPOSE can handle it.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose
  data=have(where=(missing(formula)))
  out=wide(drop=_name_ )
;
  id id;
  var score;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If it cannot you can generate CODE to make the WIDE dataset instead.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename code temp;
data _null_;
  file code;
  set have;
  where not missing(score);
  put id '=' score ';' ;
run;
data wide;
  %include code ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now use the other observations to generate code to set the SCORE (and ID and FORMUAL variables). And include the generated code to calculate the score and write the observations.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename code temp;
data _null_;
  file code;
  set have;
  where not missing(formula);
  put id=:$quote. ';' formula= :$quote. ';score=' formula ';output;' ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now read in the WIDE dataset to get the SCORE values for the IDs in question.&amp;nbsp; Also read in the HAVE dataset (but not any of the data) to set the type/length/format of the variables.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want; 
  set have(obs=0) wide;
  %inc code ;
  keep id score formula; 
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1733021705962.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/102606i7AC54B6B2DD1739F/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Tom_0-1733021705962.png" alt="Tom_0-1733021705962.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;You could generate bot code files in a single pass through the HAVE dataset.&amp;nbsp; So two passes total.&amp;nbsp; See below.&amp;nbsp; Remove the WHERE statement if you want the result to have all of the original observations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-SPOILER&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename code1 temp;
filename code2 temp;
data _null_;
  set have;
  if missing(formula) then do;
     file code1;
     put id '=' score ';' ;
  end;
  else do;
     file code2 ;
     put 'when (' id :$quote. ') score=' formula ';' ;
  end;
run;

data wide ;
  %include code1 ;
run;

data want; 
  if _n_=1 then set wide ;
  set have;
  where not missing(formula);
  select (id);
    %inc code2 ;
     otherwise ;
  end;
  keep id score formula ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/LI-SPOILER&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 01 Dec 2024 04:00:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-replace-text-from-one-column-to-another/m-p/952222#M372168</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-12-01T04:00:49Z</dc:date>
    </item>
  </channel>
</rss>

