<?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: Writing a program with a given dataset in Statistical Procedures</title>
    <link>https://communities.sas.com/t5/Statistical-Procedures/Writing-a-program-with-a-given-dataset/m-p/802859#M39457</link>
    <description>&lt;P&gt;This is code for counting all, Complete and Partial observations:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=screener_data;
by site;
run;

data want;
set screener_data;
by site;
if first.site
then do;
  count = 0;
  count_complete = 0;
  count_partial = 0;
end;
count + 1;
if vstatus = "Complete"
then count_complete + 1;
else count_partial + 1;
if last.site;
keep site count count_complete count_partial;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I leave adding the other counts as an exercise.&lt;/P&gt;</description>
    <pubDate>Fri, 18 Mar 2022 20:21:02 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2022-03-18T20:21:02Z</dc:date>
    <item>
      <title>Writing a program with a given dataset</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/Writing-a-program-with-a-given-dataset/m-p/802558#M39442</link>
      <description>&lt;P&gt;&lt;SPAN&gt;Write a program that calculates the total number of records (all screening records in the file), &lt;BR /&gt;the total number of partial records (Vstatus), the total number of complete records (Vstatus), and the total number &lt;BR /&gt;of complete records that are eligible and ineligible (ELIG) for each site (SITE). The idea here is not to create &lt;BR /&gt;separate datasets/subsets for each of these, but to produce a single dataset with these totals.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Vstatus is coded as “Complete” or “Partial”&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;ELIG is coded as 0 (no) or 1 (yes)&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;SITE is coded as “City A” or “City B”&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My professor is very specific about how she wants this done but when I write my code all my values for the "Status" variable change to "Complete" and the output is simply counting 1-50&lt;/P&gt;
&lt;P&gt;My code:&lt;/P&gt;
&lt;PRE&gt;PROC SORT DATA = sarah.screener_data; by site; RUN; &lt;BR /&gt;&lt;BR /&gt;DATA TOTVSTATUS; &lt;BR /&gt;SET Sarah.screener_data; &lt;BR /&gt;COUNT + 1;  &lt;BR /&gt;BY Site; &lt;BR /&gt;IF Vstatus = partial then COUNT=0;&lt;BR /&gt;If vstatus = complete then count= 1; &lt;BR /&gt;RUN; &lt;BR /&gt;&lt;BR /&gt;DATA TOTVSTATUS2; &lt;BR /&gt;SET TOTVSTATUS; &lt;BR /&gt;BY Vstatus Elig Site; &lt;BR /&gt;IF LAST.Vstatus ; &lt;BR /&gt;If Last.elig;&lt;BR /&gt;If last.site;&lt;BR /&gt;RUN;&lt;BR /&gt;&lt;BR /&gt;PROC SORT DATA = sarah.screener_data; by Vstatus Elig site; RUN; &lt;BR /&gt;&lt;BR /&gt;DATA TOTVSTATUS; &lt;BR /&gt;SET Sarah.screener_data; &lt;BR /&gt;COUNT + 1;  &lt;BR /&gt;BY Vstatus Elig Site; &lt;BR /&gt;IF FIRST.Vstatus then COUNT=1;  &lt;BR /&gt;RUN;&lt;BR /&gt;&lt;BR /&gt;DATA TOTVSTATUS2; &lt;BR /&gt;SET TOTVSTATUS; &lt;BR /&gt;BY Vstatus Elig Site; &lt;BR /&gt;IF LAST.Vstatus ;&lt;BR /&gt;run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 17 Mar 2022 04:11:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/Writing-a-program-with-a-given-dataset/m-p/802558#M39442</guid>
      <dc:creator>saza</dc:creator>
      <dc:date>2022-03-17T04:11:33Z</dc:date>
    </item>
    <item>
      <title>Re: Writing a program with a given dataset</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/Writing-a-program-with-a-given-dataset/m-p/802560#M39443</link>
      <description>&lt;P&gt;Can you explain the logic for the code you posted?&amp;nbsp; What is it you think it is going to do?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is an analysis of the first data step you posted.&lt;/P&gt;
&lt;LI-SPOILER&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA TOTVSTATUS; 
  SET Sarah.screener_data; 
  COUNT + 1;  
  BY Site; 
  IF Vstatus = partial then COUNT=0;
  If vstatus = complete then count= 1; 
RUN; &lt;/CODE&gt;&lt;/PRE&gt;
Why did you insert the sum statement to increment COUNT between the SET statement and the BY statement?&lt;BR /&gt;Why did you include the BY statement when you do not make any use of it?&amp;nbsp; The BY statement will cause the SET statement to create a populate FIRST.SITE and LAST.SITE boolean flags, but your data step is not using them.&lt;BR /&gt;What are the values of PARTIAL and COMPLETE variables you are referencing in your IF statements?&amp;nbsp; You did not mention such variables in your description.&amp;nbsp; Since you claim that COUNT just runs from 1 to 50 the values of PARTIAL and COMPLETE must never match the value of VSTATUS or else COUNT would have been reset to 0 or 1 before incrementing again the time the sum statement executed.&lt;/LI-SPOILER&gt;</description>
      <pubDate>Thu, 17 Mar 2022 04:20:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/Writing-a-program-with-a-given-dataset/m-p/802560#M39443</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-03-17T04:20:14Z</dc:date>
    </item>
    <item>
      <title>Re: Writing a program with a given dataset</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/Writing-a-program-with-a-given-dataset/m-p/802567#M39444</link>
      <description>Yes I realize now how ridiculous my code is. I guess what i'm confused about is the count variable and if I can have count=1 for everything that i'm trying to add. for example, the "Vstatus" variable is either 'complete' or 'partial'&lt;BR /&gt;&lt;BR /&gt;So can my code be: &lt;BR /&gt;DATA TOTVSTATUS; &lt;BR /&gt;SET Sarah.screener_data; &lt;BR /&gt;COUNT + 1;  &lt;BR /&gt;BY Site; &lt;BR /&gt;IF Vstatus = partial then COUNT=1;&lt;BR /&gt;If vstatus = complete then count= 1; &lt;BR /&gt;RUN; &lt;BR /&gt;&lt;BR /&gt;I guess i'm trying to speak to my sas code in english lol&lt;BR /&gt;</description>
      <pubDate>Thu, 17 Mar 2022 04:34:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/Writing-a-program-with-a-given-dataset/m-p/802567#M39444</guid>
      <dc:creator>saza</dc:creator>
      <dc:date>2022-03-17T04:34:39Z</dc:date>
    </item>
    <item>
      <title>Re: Writing a program with a given dataset</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/Writing-a-program-with-a-given-dataset/m-p/802571#M39445</link>
      <description>&lt;P&gt;In your post, you state this:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;Vstatus is coded as “Complete” or “Partial”&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;But in your code, you do this:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;IF Vstatus = partial then COUNT=0;
If vstatus = complete then count= 1; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;partial is a variable name, while "partial" is a value. I guess you want to test for values, not other variables.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Read the log (Maxim 2). It will probably alert you to the fact that variables partial and complete are uninitialized.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 17 Mar 2022 05:30:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/Writing-a-program-with-a-given-dataset/m-p/802571#M39445</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-03-17T05:30:50Z</dc:date>
    </item>
    <item>
      <title>Re: Writing a program with a given dataset</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/Writing-a-program-with-a-given-dataset/m-p/802853#M39455</link>
      <description>I also realized that when I write partial and complete I need to put them in '' so sas doesn't add them as a variable on my results. &lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;DATA TOTVSTATUS; &lt;BR /&gt;SET Sarah.screener_data; &lt;BR /&gt;COUNT + 1;  &lt;BR /&gt;BY Site; &lt;BR /&gt;IF Vstatus = 'partial' then COUNT=0;&lt;BR /&gt;If Vstatus = 'complete' then COUNT=0;&lt;BR /&gt;IF Elig = 0 then count= 1;&lt;BR /&gt;If Elig = 1 then count=1;  &lt;BR /&gt;RUN;&lt;BR /&gt;&lt;BR /&gt;When I run this my count is all messed up but atleast sas isn't making new variables</description>
      <pubDate>Fri, 18 Mar 2022 19:22:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/Writing-a-program-with-a-given-dataset/m-p/802853#M39455</guid>
      <dc:creator>saza</dc:creator>
      <dc:date>2022-03-18T19:22:20Z</dc:date>
    </item>
    <item>
      <title>Re: Writing a program with a given dataset</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/Writing-a-program-with-a-given-dataset/m-p/802855#M39456</link>
      <description>&lt;P&gt;Pretend you are the SAS data step processor and the input to your data step was this data:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input site $ vstatus $ elig ;
cards;
HERE partial 0
HERE complete 0
HERE parital 1
HERE complete 1
THERE partial 0
THERE complete 0
THERE parital 1
THERE complete 1
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;What would you WANT as the output?&lt;/P&gt;
&lt;P&gt;What would you expect as the output from your data step?&lt;/P&gt;
&lt;LI-SPOILER&gt;
&lt;PRE&gt;85    DATA TOTVSTATUS;
86    SET have;
87    COUNT + 1;
88    BY Site;
89    IF Vstatus = 'partial' then COUNT=0;
90    If Vstatus = 'complete' then COUNT=0;
91    IF Elig = 0 then count= 1;
92    If Elig = 1 then count=1;
93    put (_all_) (=);
94    RUN;

site=HERE vstatus=partial elig=0 COUNT=1
site=HERE vstatus=complete elig=0 COUNT=1
site=HERE vstatus=parital elig=1 COUNT=1
site=HERE vstatus=complete elig=1 COUNT=1
site=THERE vstatus=partial elig=0 COUNT=1
site=THERE vstatus=complete elig=0 COUNT=1
site=THERE vstatus=parital elig=1 COUNT=1
site=THERE vstatus=complete elig=1 COUNT=1
&lt;/PRE&gt;
&lt;/LI-SPOILER&gt;
&lt;P&gt;What would you expect the value of COUNT to be after each of those statements?&lt;/P&gt;
&lt;LI-SPOILER&gt;
&lt;PRE&gt;142   DATA TOTVSTATUS;
143   SET have;
144   put (_n_ site vstatus elig count) (=) @;
145   COUNT + 1;
146   put '-&amp;gt;' count @;
147   BY Site;
148   IF Vstatus = 'partial' then COUNT=0;
149   put '-&amp;gt;' count @;
150   If Vstatus = 'complete' then COUNT=0;
151   put '-&amp;gt;' count @;
152   IF Elig = 0 then count= 1;
153   put '-&amp;gt;' count @;
154   If Elig = 1 then count=1;
155   put '-&amp;gt;' count @;
156   put;
157   RUN;

_N_=1 site=HERE vstatus=partial elig=0 count=0 -&amp;gt;1 -&amp;gt;0 -&amp;gt;0 -&amp;gt;1 -&amp;gt;1
_N_=2 site=HERE vstatus=complete elig=0 count=1 -&amp;gt;2 -&amp;gt;2 -&amp;gt;0 -&amp;gt;1 -&amp;gt;1
_N_=3 site=HERE vstatus=parital elig=1 count=1 -&amp;gt;2 -&amp;gt;2 -&amp;gt;2 -&amp;gt;2 -&amp;gt;1
_N_=4 site=HERE vstatus=complete elig=1 count=1 -&amp;gt;2 -&amp;gt;2 -&amp;gt;0 -&amp;gt;0 -&amp;gt;1
_N_=5 site=THERE vstatus=partial elig=0 count=1 -&amp;gt;2 -&amp;gt;0 -&amp;gt;0 -&amp;gt;1 -&amp;gt;1
_N_=6 site=THERE vstatus=complete elig=0 count=1 -&amp;gt;2 -&amp;gt;2 -&amp;gt;0 -&amp;gt;1 -&amp;gt;1
_N_=7 site=THERE vstatus=parital elig=1 count=1 -&amp;gt;2 -&amp;gt;2 -&amp;gt;2 -&amp;gt;2 -&amp;gt;1
_N_=8 site=THERE vstatus=complete elig=1 count=1 -&amp;gt;2 -&amp;gt;2 -&amp;gt;0 -&amp;gt;0 -&amp;gt;1
&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>Fri, 18 Mar 2022 19:37:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/Writing-a-program-with-a-given-dataset/m-p/802855#M39456</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-03-18T19:37:47Z</dc:date>
    </item>
    <item>
      <title>Re: Writing a program with a given dataset</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/Writing-a-program-with-a-given-dataset/m-p/802859#M39457</link>
      <description>&lt;P&gt;This is code for counting all, Complete and Partial observations:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=screener_data;
by site;
run;

data want;
set screener_data;
by site;
if first.site
then do;
  count = 0;
  count_complete = 0;
  count_partial = 0;
end;
count + 1;
if vstatus = "Complete"
then count_complete + 1;
else count_partial + 1;
if last.site;
keep site count count_complete count_partial;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I leave adding the other counts as an exercise.&lt;/P&gt;</description>
      <pubDate>Fri, 18 Mar 2022 20:21:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/Writing-a-program-with-a-given-dataset/m-p/802859#M39457</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-03-18T20:21:02Z</dc:date>
    </item>
    <item>
      <title>Re: Writing a program with a given dataset</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/Writing-a-program-with-a-given-dataset/m-p/802865#M39459</link>
      <description>I was able to add the other variables and like your way of doing this better than the one I was taught! very straightforward. Thanks again.</description>
      <pubDate>Fri, 18 Mar 2022 20:38:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/Writing-a-program-with-a-given-dataset/m-p/802865#M39459</guid>
      <dc:creator>saza</dc:creator>
      <dc:date>2022-03-18T20:38:21Z</dc:date>
    </item>
    <item>
      <title>Re: Writing a program with a given dataset</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/Writing-a-program-with-a-given-dataset/m-p/802867#M39460</link>
      <description>&lt;P&gt;Good answer.&lt;/P&gt;
&lt;P&gt;Your indentation style is hard to read through.&lt;/P&gt;
&lt;P&gt;Can you tell at glance the difference between these two lines from your program?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if first.site
if last.site;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you use a more normal indentation style it would be easier.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if first.site then do;
  count = 0;
  count_complete = 0;
  count_partial = 0;
end;
count + 1;
if vstatus = "Complete" then count_complete + 1;
else count_partial + 1;
if last.site;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you insist on splitting your IF/THEN statements into more than one line you could indent and move the termination of the multiple line statement to the start of a new line to make it easier to see where the multiple line statement ends.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if first.site
  then do
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I always wonder why you are creating a variable named THEN when I first look at that code.&lt;/P&gt;</description>
      <pubDate>Fri, 18 Mar 2022 21:23:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/Writing-a-program-with-a-given-dataset/m-p/802867#M39460</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-03-18T21:23:21Z</dc:date>
    </item>
    <item>
      <title>Re: Writing a program with a given dataset</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/Writing-a-program-with-a-given-dataset/m-p/802870#M39461</link>
      <description>&lt;P&gt;I have a rationale for my way of writing the IF this way.&lt;/P&gt;
&lt;P&gt;Conditional execution consists of at least two, often three parts: the condition, the "true" branch, and the "false" branch.&lt;/P&gt;
&lt;P&gt;My way of writing it reflects this by having three lines (or three blocks). With a quite complex condition, this then looks like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if
  &amp;lt;condition&amp;gt;
then do;
  &amp;lt;statements&amp;gt;
end;
else do;
  &amp;lt;statements&amp;gt;
end;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 18 Mar 2022 21:33:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/Writing-a-program-with-a-given-dataset/m-p/802870#M39461</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-03-18T21:33:04Z</dc:date>
    </item>
    <item>
      <title>Re: Writing a program with a given dataset</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/Writing-a-program-with-a-given-dataset/m-p/802924#M39466</link>
      <description>&lt;P&gt;Still don't get it.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you don't split the single IF statement into multiple lines then the blocks are still there.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if &amp;lt;condition&amp;gt; then do;
  &amp;lt;statements&amp;gt;
end;
else do;
  &amp;lt;statements&amp;gt;
end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;or&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if &amp;lt;condition&amp;gt; then 
  &amp;lt;statement&amp;gt;
;
else
  &amp;lt;statement&amp;gt;
;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 19 Mar 2022 16:57:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/Writing-a-program-with-a-given-dataset/m-p/802924#M39466</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-03-19T16:57:41Z</dc:date>
    </item>
    <item>
      <title>Re: Writing a program with a given dataset</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/Writing-a-program-with-a-given-dataset/m-p/802929#M39467</link>
      <description>&lt;P&gt;I like the THEN keyword to be on the same column as the ELSE, as they are related. Both start one if the branches.&lt;/P&gt;
&lt;P&gt;Look at this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if first.id
then count = 1;
else count + 1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Looks better in my eyes than&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if first.id then count = 1;
else count + 1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 19 Mar 2022 17:22:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/Writing-a-program-with-a-given-dataset/m-p/802929#M39467</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-03-19T17:22:56Z</dc:date>
    </item>
  </channel>
</rss>

