<?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: extract value of first variable containing specific value into a new variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/extract-value-of-first-variable-containing-specific-value-into-a/m-p/713577#M220164</link>
    <description>&lt;P&gt;First transpose to a long dataset, with matnr and code (keep an additional running position number if that is important). Then it is only a WHERE and a count:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
where substr(code,1,3) = "ETT";
by matnr;
if first.matnr
then ett_count = 1;
else ett_count + 1;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sat, 23 Jan 2021 08:42:22 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2021-01-23T08:42:22Z</dc:date>
    <item>
      <title>extract value of first variable containing specific value into a new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extract-value-of-first-variable-containing-specific-value-into-a/m-p/713576#M220163</link>
      <description>&lt;P&gt;I have a dataset with tons of material numbers who can have up to 44 codes each (all of 6 characters). I want to create new variables (ETT_1, ETT_2 and so on) which take the value of the codes starting with "ETT". Some of the materialnumbers can have &amp;gt;1 codes containing "ETT", why I have maked a count variable (count_ETT). So I should make a syntax for ETT_1 which says "if count_ETT=1 then ETT_1= the value of the first code (code_1-code_44) containing "ETT"), and a make a syntax for ETT_2 which says "if count_ETT=2 then ETT_2= the value of the second code (code_1-code_44) containing "ETT"). Is this possible??&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Matnr&lt;/TD&gt;&lt;TD&gt;Code_1&lt;/TD&gt;&lt;TD&gt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&lt;/TABLE&gt;</description>
      <pubDate>Sat, 23 Jan 2021 08:18:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extract-value-of-first-variable-containing-specific-value-into-a/m-p/713576#M220163</guid>
      <dc:creator>MABRINCH</dc:creator>
      <dc:date>2021-01-23T08:18:43Z</dc:date>
    </item>
    <item>
      <title>Re: extract value of first variable containing specific value into a new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extract-value-of-first-variable-containing-specific-value-into-a/m-p/713577#M220164</link>
      <description>&lt;P&gt;First transpose to a long dataset, with matnr and code (keep an additional running position number if that is important). Then it is only a WHERE and a count:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
where substr(code,1,3) = "ETT";
by matnr;
if first.matnr
then ett_count = 1;
else ett_count + 1;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 23 Jan 2021 08:42:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extract-value-of-first-variable-containing-specific-value-into-a/m-p/713577#M220164</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-01-23T08:42:22Z</dc:date>
    </item>
    <item>
      <title>Re: extract value of first variable containing specific value into a new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extract-value-of-first-variable-containing-specific-value-into-a/m-p/713578#M220165</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
 set have end=eof;
 retain max_count 0;
     array cd {*} code_1-code_44;
	 array et {*} ETT_1-ETT_44;
	 count_ETT = 0;
	 do i=1 to dim(cd);
	    if substr(cd(i),1,3='ETT') then do;
		   count_ETT +1; 
		   max_count = mac(max_count, count_ETT;)
		   et(count_ETT) = cd(i);
		end;
	 end;
	 drop i;
	 if eof and max_count &amp;lt; 44 then do;
	    put "Variables ETT_n for n &amp;gt; " max_count " can be drpped";
		call symput('Max_Count', put(max_count,2.));
	 end;
run;
/*** Drop exess ETT_ varaibles ***/
%macro dropx;
   data want;
    set want;
	    drop
    %do i=&amp;amp;max_count to 44;
       ETT_&amp;amp;i%str( );
    %end; ;
   run;
%mend;
%dropx;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 23 Jan 2021 08:55:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extract-value-of-first-variable-containing-specific-value-into-a/m-p/713578#M220165</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2021-01-23T08:55:27Z</dc:date>
    </item>
    <item>
      <title>Re: extract value of first variable containing specific value into a new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extract-value-of-first-variable-containing-specific-value-into-a/m-p/713618#M220181</link>
      <description>&lt;P&gt;the code won't work. &lt;SPAN&gt;I've changed places in the code where I think you've made typos, but still won't work. See code, log and table ( I made a demo with only code_1 to code_4. I'm working on a secured server, so can't copy the SAScode directly in ).&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV class="mceNonEditable lia-copypaste-placeholder"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class="mceNonEditable lia-copypaste-placeholder"&gt;&amp;nbsp;&lt;/DIV&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="11.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/53836i1FF3FB6B94DFF02F/image-size/large?v=v2&amp;amp;px=999" role="button" title="11.png" alt="11.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;DIV class="mceNonEditable lia-copypaste-placeholder"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class="mceNonEditable lia-copypaste-placeholder"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class="mceNonEditable lia-copypaste-placeholder"&gt;&amp;nbsp;&lt;/DIV&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;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 23 Jan 2021 19:23:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extract-value-of-first-variable-containing-specific-value-into-a/m-p/713618#M220181</guid>
      <dc:creator>MABRINCH</dc:creator>
      <dc:date>2021-01-23T19:23:50Z</dc:date>
    </item>
    <item>
      <title>Re: extract value of first variable containing specific value into a new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extract-value-of-first-variable-containing-specific-value-into-a/m-p/713619#M220182</link>
      <description>&lt;P&gt;Seems like a waste of time but pretty simple.&amp;nbsp; Make an array of the input variables and another array to define the output variables.&amp;nbsp; Then loop over the input variables checking for the ETT values.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
  input Matnr (Code_1-Code_5) (:$8.) ;
cards;
1 P40000 T98000 ETT800 T98000   ETT600 
2 P70000 T30300 M87700 ETT600   T98000 
3 P33000 ETT600 M87710 T55500   T98000 
;

data want;
  set have ;
  array in code_1-code_5;
  array out $8 ett_1-ett_5;
  count_ett=0;
  do index=1 to dim(in);
    if in[index]=:'ETT' then do;
      count_ett+1;
      out[count_ett]=in[index];
    end;
  end;
  drop index;
run;

proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;                                                                                                     count_
Obs   Matnr   Code_1   Code_2   Code_3   Code_4   Code_5   ett_1    ett_2    ett_3   ett_4   ett_5     ett

 1      1     P40000   T98000   ETT800   T98000   ETT600   ETT800   ETT600                              2
 2      2     P70000   T30300   M87700   ETT600   T98000   ETT600                                       1
 3      3     P33000   ETT600   M87710   T55500   T98000   ETT600                                       1&lt;/PRE&gt;</description>
      <pubDate>Sat, 23 Jan 2021 20:08:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extract-value-of-first-variable-containing-specific-value-into-a/m-p/713619#M220182</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-01-23T20:08:13Z</dc:date>
    </item>
    <item>
      <title>Re: extract value of first variable containing specific value into a new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extract-value-of-first-variable-containing-specific-value-into-a/m-p/713623#M220185</link>
      <description>&lt;P&gt;1) It would be easier to test a code if you supply a test data using data step with infile statement.&lt;/P&gt;
&lt;P&gt;2) I hope next fixed code will work. See remarks on updated lines:&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;PRE&gt;%let max_count = 0;  /* initial value */
data want;
 set have end=eof;
     retain max_count 0;
	 length code_1-code_44 $6; /* line added */
     array cd {*} $ code_1-code_44;  /* $ added */
	 array et {*} ETT_1-ETT_44;
	 count_ETT = 0;
	 do i=1 to dim(cd);
	    if substr(cd(i),1,3='ETT') then do;
		   count_ETT +1; 
		   max_count = mac(max_count, count_ETT;)
		   et(count_ETT) = cd(i);
		end;
	 end;
	 drop i;
	 if eof and max_count &amp;lt; 44 then do;
	    put "Variables ETT_n for n &amp;gt; " max_count " can be drpped";
		call symput('Max_Count', put(max_count,2.));
	 end;
run;
/*** Drop exess ETT_ varaibles ***/
%macro dropx;
  %if &amp;amp;max_count=0 %then %goto exit;
   data want;
    set want;
	    drop
    %do i=&amp;amp;max_count to 44;
       ETT_&amp;amp;i%str( );
    %end; ;
   run;
%exit:
%mend;
%dropx;&lt;/PRE&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 23 Jan 2021 20:25:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extract-value-of-first-variable-containing-specific-value-into-a/m-p/713623#M220185</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2021-01-23T20:25:20Z</dc:date>
    </item>
    <item>
      <title>Re: extract value of first variable containing specific value into a new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extract-value-of-first-variable-containing-specific-value-into-a/m-p/713642#M220192</link>
      <description>&lt;P&gt;Thank you Tom !&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;The ETT code is a code for thickness in mm, where the numbers are mm from 0,01 mm to 20 mm:&lt;/P&gt;&lt;P&gt;ETT001=0,01 mm&lt;/P&gt;&lt;P&gt;ETT002=0,02 mm&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;ETT100=1,00 mm&amp;nbsp;&lt;/P&gt;&lt;P&gt;...&amp;nbsp;&lt;/P&gt;&lt;P&gt;ETT999= 9,99 mm&lt;/P&gt;&lt;P&gt;ETTX10=10 mm&amp;nbsp;&lt;/P&gt;&lt;P&gt;ETTX11= 11 mm&lt;/P&gt;&lt;P&gt;...&lt;BR /&gt;ETTX20= 20 mm&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Is it possible to convert the codes to mm ?&lt;/P&gt;</description>
      <pubDate>Sat, 23 Jan 2021 22:31:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extract-value-of-first-variable-containing-specific-value-into-a/m-p/713642#M220192</guid>
      <dc:creator>MABRINCH</dc:creator>
      <dc:date>2021-01-23T22:31:55Z</dc:date>
    </item>
    <item>
      <title>Re: extract value of first variable containing specific value into a new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extract-value-of-first-variable-containing-specific-value-into-a/m-p/713647#M220195</link>
      <description>&lt;P&gt;Are the mappings arbitrary or is there a pattern.&lt;/P&gt;
&lt;P&gt;If the arbitrary the make a format.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format ;
  value $ett2mm 
'ETT001'='0,01 mm'
... 
'ETTX20'= '20 mm'
   ;
run; 
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You could then either attach the format to the variables.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;format ett: $ett2mm.&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or use it in the step that is moving the values.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;out[count_ett]=put(in[index],$ett2mm.);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If there is a pattern then you can use input to read the values from the end of the strings into actual numbers. You can get INPUT to imply the decimal point by adding .2 to the end of the informat specification.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;...
array out ett_1-ett_5;
...
if in[index] =: 'ETTX' then out[count_ett]=input(substr(in[index],5),32.);
else out[count_ett]=input(substr(in[index],4),32.2);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 23 Jan 2021 23:01:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extract-value-of-first-variable-containing-specific-value-into-a/m-p/713647#M220195</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-01-23T23:01:51Z</dc:date>
    </item>
    <item>
      <title>Re: extract value of first variable containing specific value into a new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extract-value-of-first-variable-containing-specific-value-into-a/m-p/713671#M220211</link>
      <description>&lt;P&gt;There is a patteren.&amp;nbsp;&lt;/P&gt;&lt;P&gt;What should I fill in where you typed "...", sorry. (I am very new to SAS, normally using SPSS).&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;array in ett_1-ett_5;
array out ett_1-ett_5;
do index=1 to dim(in);
if in[index] =: 'ETTX' then out[count_ett]=input(substr(in[index],5),32.);
else out[count_ett]=input(substr(in[index],4),32.2);
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 24 Jan 2021 09:12:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extract-value-of-first-variable-containing-specific-value-into-a/m-p/713671#M220211</guid>
      <dc:creator>MABRINCH</dc:creator>
      <dc:date>2021-01-24T09:12:37Z</dc:date>
    </item>
  </channel>
</rss>

