<?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 Assign values using Do loops in a larger dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Assign-values-using-Do-loops-in-a-larger-dataset/m-p/639405#M190232</link>
    <description>&lt;P&gt;Hi sas experts,&amp;nbsp; I have a dataset&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data example;&lt;/P&gt;
&lt;P&gt;input var1 var2 var3;cards;&lt;/P&gt;
&lt;P&gt;01 MAP 0&amp;nbsp;&lt;/P&gt;
&lt;P&gt;01 MAP 1&lt;/P&gt;
&lt;P&gt;01 CVP 0&lt;/P&gt;
&lt;P&gt;01 CVP 1&lt;/P&gt;
&lt;P&gt;01 CVP 2&lt;/P&gt;
&lt;P&gt;01 GCS 0&lt;/P&gt;
&lt;P&gt;01 GCS 1&lt;/P&gt;
&lt;P&gt;01 HR 0&lt;/P&gt;
&lt;P&gt;01 HR 1&lt;/P&gt;
&lt;P&gt;02 MAP 0&lt;/P&gt;
&lt;P&gt;02 MAP 1&lt;/P&gt;
&lt;P&gt;02 MAP 2&lt;/P&gt;
&lt;P&gt;02 MAP 3&lt;/P&gt;
&lt;P&gt;02 CVP 0&lt;/P&gt;
&lt;P&gt;02 CVP 1&lt;/P&gt;
&lt;P&gt;02 CVP 2&lt;/P&gt;
&lt;P&gt;02 CVP 3&lt;/P&gt;
&lt;P&gt;02 GCS 0&amp;nbsp;&lt;/P&gt;
&lt;P&gt;02 GCS 1&lt;/P&gt;
&lt;P&gt;02 HR 0&lt;/P&gt;
&lt;P&gt;02 HR 1&lt;/P&gt;
&lt;P&gt;02 HR 2&amp;nbsp;&lt;/P&gt;
&lt;P&gt;03 MAP 0&lt;/P&gt;
&lt;P&gt;03 MAP 1&lt;/P&gt;
&lt;P&gt;03 CVP 0&lt;/P&gt;
&lt;P&gt;03 GCS 0&lt;/P&gt;
&lt;P&gt;;run;&lt;/P&gt;
&lt;P&gt;in the dataset i have numerical ordering variable (var3) for by each var1 var2, its a sorted dataset. I want to create a new variable where i can assign values such as:&amp;nbsp; form 01 to last category obs of var1(084 in my actual dataset), any no of times MAP appears in the observation the newvar= would be 1 and similarly CVP=2, GCS=3, HR=4.&lt;/P&gt;
&lt;P&gt;please suggest how to solve this using do loop. Also it would be really helpful if any expert could guide for adding a counter in loop for the below code, as it works only for 01 and MAP and for other observations it simply takes the row number.&lt;/P&gt;
&lt;P&gt;data nDATA;&lt;BR /&gt;set example;&lt;BR /&gt;retain newvar;&lt;BR /&gt;by var1 var2;&lt;BR /&gt;if first.var2 then newvar=_n_;&lt;BR /&gt;output;&lt;BR /&gt;run;&lt;/P&gt;</description>
    <pubDate>Mon, 13 Apr 2020 05:26:03 GMT</pubDate>
    <dc:creator>sahoositaram555</dc:creator>
    <dc:date>2020-04-13T05:26:03Z</dc:date>
    <item>
      <title>Assign values using Do loops in a larger dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assign-values-using-Do-loops-in-a-larger-dataset/m-p/639405#M190232</link>
      <description>&lt;P&gt;Hi sas experts,&amp;nbsp; I have a dataset&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data example;&lt;/P&gt;
&lt;P&gt;input var1 var2 var3;cards;&lt;/P&gt;
&lt;P&gt;01 MAP 0&amp;nbsp;&lt;/P&gt;
&lt;P&gt;01 MAP 1&lt;/P&gt;
&lt;P&gt;01 CVP 0&lt;/P&gt;
&lt;P&gt;01 CVP 1&lt;/P&gt;
&lt;P&gt;01 CVP 2&lt;/P&gt;
&lt;P&gt;01 GCS 0&lt;/P&gt;
&lt;P&gt;01 GCS 1&lt;/P&gt;
&lt;P&gt;01 HR 0&lt;/P&gt;
&lt;P&gt;01 HR 1&lt;/P&gt;
&lt;P&gt;02 MAP 0&lt;/P&gt;
&lt;P&gt;02 MAP 1&lt;/P&gt;
&lt;P&gt;02 MAP 2&lt;/P&gt;
&lt;P&gt;02 MAP 3&lt;/P&gt;
&lt;P&gt;02 CVP 0&lt;/P&gt;
&lt;P&gt;02 CVP 1&lt;/P&gt;
&lt;P&gt;02 CVP 2&lt;/P&gt;
&lt;P&gt;02 CVP 3&lt;/P&gt;
&lt;P&gt;02 GCS 0&amp;nbsp;&lt;/P&gt;
&lt;P&gt;02 GCS 1&lt;/P&gt;
&lt;P&gt;02 HR 0&lt;/P&gt;
&lt;P&gt;02 HR 1&lt;/P&gt;
&lt;P&gt;02 HR 2&amp;nbsp;&lt;/P&gt;
&lt;P&gt;03 MAP 0&lt;/P&gt;
&lt;P&gt;03 MAP 1&lt;/P&gt;
&lt;P&gt;03 CVP 0&lt;/P&gt;
&lt;P&gt;03 GCS 0&lt;/P&gt;
&lt;P&gt;;run;&lt;/P&gt;
&lt;P&gt;in the dataset i have numerical ordering variable (var3) for by each var1 var2, its a sorted dataset. I want to create a new variable where i can assign values such as:&amp;nbsp; form 01 to last category obs of var1(084 in my actual dataset), any no of times MAP appears in the observation the newvar= would be 1 and similarly CVP=2, GCS=3, HR=4.&lt;/P&gt;
&lt;P&gt;please suggest how to solve this using do loop. Also it would be really helpful if any expert could guide for adding a counter in loop for the below code, as it works only for 01 and MAP and for other observations it simply takes the row number.&lt;/P&gt;
&lt;P&gt;data nDATA;&lt;BR /&gt;set example;&lt;BR /&gt;retain newvar;&lt;BR /&gt;by var1 var2;&lt;BR /&gt;if first.var2 then newvar=_n_;&lt;BR /&gt;output;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Mon, 13 Apr 2020 05:26:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assign-values-using-Do-loops-in-a-larger-dataset/m-p/639405#M190232</guid>
      <dc:creator>sahoositaram555</dc:creator>
      <dc:date>2020-04-13T05:26:03Z</dc:date>
    </item>
    <item>
      <title>Re: Assign values using Do loops in a larger dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assign-values-using-Do-loops-in-a-larger-dataset/m-p/639406#M190233</link>
      <description>&lt;P&gt;There is no reason for a do loop, as the data step already&amp;nbsp;&lt;EM&gt;is&lt;/EM&gt; a loop when reading a dataset (or an external file).&lt;/P&gt;
&lt;P&gt;_n_ is an automatic variable that is set to the number of the current data step iteration at the beginning of the iteration.&lt;/P&gt;
&lt;P&gt;What you need is a retained variable that is set when var2 changes:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data nDATA;
set example;
retain newvar;
by var1 var2 notsorted;
if first.var2
then if var2 = 'MAP'
  then newvar = 1;
  else newvar + 1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that the data step executes an implicit output at the end of each iteration if no explicit output statement is present.&lt;/P&gt;
&lt;P&gt;Because an increment statement is used, you could omit the retain statement; variables incremented in this way are automatically retained.&lt;/P&gt;</description>
      <pubDate>Mon, 13 Apr 2020 05:43:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assign-values-using-Do-loops-in-a-larger-dataset/m-p/639406#M190233</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-04-13T05:43:41Z</dc:date>
    </item>
    <item>
      <title>Re: Assign values using Do loops in a larger dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assign-values-using-Do-loops-in-a-larger-dataset/m-p/639408#M190234</link>
      <description>Hi &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;,  Thanks for your help.Sorry to say but, newvar is retaining only 1 through out entire observations. Also below statement from your code , i'm curious to know can we use two if statements like that ? Please help me know.&lt;BR /&gt;if first.var2&lt;BR /&gt;then if var2 = 'MAP'</description>
      <pubDate>Mon, 13 Apr 2020 05:58:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assign-values-using-Do-loops-in-a-larger-dataset/m-p/639408#M190234</guid>
      <dc:creator>sahoositaram555</dc:creator>
      <dc:date>2020-04-13T05:58:03Z</dc:date>
    </item>
    <item>
      <title>Re: Assign values using Do loops in a larger dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assign-values-using-Do-loops-in-a-larger-dataset/m-p/639411#M190236</link>
      <description>&lt;P&gt;First of all, software testing is a virtue and not a crime. It is not forbidden by any country's constitution that I know of.&lt;/P&gt;
&lt;P&gt;Therefore you MUST(!!!) test code that you post for example data. Had you done this, it would have revealed that one cannot read values like MAP into a numeric variable.&lt;/P&gt;
&lt;P&gt;With slightly corrected example data code (note the $ to read var2 as character), my works as intended:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data example;
input var1 var2 $ var3;
cards;
01 MAP 0 
01 MAP 1
01 CVP 0
01 CVP 1
01 CVP 2
01 GCS 0
01 GCS 1
01 HR 0
01 HR 1
02 MAP 0
02 MAP 1
02 MAP 2
02 MAP 3
02 CVP 0
02 CVP 1
02 CVP 2
02 CVP 3
02 GCS 0 
02 GCS 1
02 HR 0
02 HR 1
02 HR 2 
03 MAP 0
03 MAP 1
03 CVP 0
03 GCS 0
;

data nDATA;
set example;
retain newvar;
by var1 var2 notsorted;
if first.var2
then if var2 = 'MAP'
  then newvar = 1;
  else newvar + 1;
run;

proc print data=ndata noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;var1	var2	var3	newvar
1	MAP	0	1
1	MAP	1	1
1	CVP	0	2
1	CVP	1	2
1	CVP	2	2
1	GCS	0	3
1	GCS	1	3
1	HR	0	4
1	HR	1	4
2	MAP	0	1
2	MAP	1	1
2	MAP	2	1
2	MAP	3	1
2	CVP	0	2
2	CVP	1	2
2	CVP	2	2
2	CVP	3	2
2	GCS	0	3
2	GCS	1	3
2	HR	0	4
2	HR	1	4
2	HR	2	4
3	MAP	0	1
3	MAP	1	1
3	CVP	0	2
3	GCS	0	3
&lt;/PRE&gt;</description>
      <pubDate>Mon, 13 Apr 2020 06:58:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assign-values-using-Do-loops-in-a-larger-dataset/m-p/639411#M190236</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-04-13T06:58:40Z</dc:date>
    </item>
    <item>
      <title>Re: Assign values using Do loops in a larger dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assign-values-using-Do-loops-in-a-larger-dataset/m-p/639412#M190237</link>
      <description>Thank you very much &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;, Noted your points. I agree.</description>
      <pubDate>Mon, 13 Apr 2020 07:09:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assign-values-using-Do-loops-in-a-larger-dataset/m-p/639412#M190237</guid>
      <dc:creator>sahoositaram555</dc:creator>
      <dc:date>2020-04-13T07:09:26Z</dc:date>
    </item>
  </channel>
</rss>

