<?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: For each individual, counting how many variable have a specific value in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/For-each-individual-counting-how-many-variable-have-a-specific/m-p/706018#M216658</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
  select
    id,
    sum(case
      when col1 = 1 and upcase(_name_) like '%Y'
      then 1
      else 0
    end) as number_of_admissions_to_hospital
    sum(case
      when col1 = 1 and upcase(_name_) like '%INF'
      then 1
      else 0
    end) as number_of_infections
  from long
  group by id
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Add additional conditions as needed to the CASE clauses.&lt;/P&gt;</description>
    <pubDate>Tue, 15 Dec 2020 13:23:20 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2020-12-15T13:23:20Z</dc:date>
    <item>
      <title>For each individual, counting how many variable have a specific value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/For-each-individual-counting-how-many-variable-have-a-specific/m-p/705554#M216479</link>
      <description>&lt;P&gt;I have a hospital data set with a list of 18 variables showing whether each patient was admitted to the hospital in the last 18 years (1=yes, 2=no) (I have some other variables that I do not want to change). In a new variable (number_of_admissions_to_hospital) I want to count how many times each patient was admitted to the hospital in the last 18 years.&lt;/P&gt;
&lt;P&gt;ID Y_2000&amp;nbsp; Y_2001&amp;nbsp;&amp;nbsp;Y_2002 .... Y_2018&amp;nbsp; &amp;nbsp;number_of_admissions_to_hospital&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;
&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I appreciate it if you help me to add&amp;nbsp;&lt;STRONG&gt;number_of_admissions_to_hospital &lt;/STRONG&gt;to the variables of my data set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 13 Dec 2020 22:12:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/For-each-individual-counting-how-many-variable-have-a-specific/m-p/705554#M216479</guid>
      <dc:creator>Emma_at_SAS</dc:creator>
      <dc:date>2020-12-13T22:12:32Z</dc:date>
    </item>
    <item>
      <title>Re: For each individual, counting how many variable have a specific value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/For-each-individual-counting-how-many-variable-have-a-specific/m-p/705558#M216480</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=have out=long (drop=_name_);
by id;
var y_:;
run;

proc sql;
create table want as
  select
    id,
    sum(case
      when col1 = 1
      then 1
      else 0
    end) as number_of_admissions_to_hospital
  from long
  group by id
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;As usual, a long dataset layout makes the coding simple.&lt;/P&gt;</description>
      <pubDate>Sun, 13 Dec 2020 22:54:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/For-each-individual-counting-how-many-variable-have-a-specific/m-p/705558#M216480</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-12-13T22:54:56Z</dc:date>
    </item>
    <item>
      <title>Re: For each individual, counting how many variable have a specific value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/For-each-individual-counting-how-many-variable-have-a-specific/m-p/705560#M216481</link>
      <description>&lt;P&gt;Thank you,&amp;nbsp;&lt;A class="trigger-hovercard" href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562" target="_blank"&gt;KurtBremser&lt;/A&gt;. So, with other variables in my data set, how does the long version of the data works? Then I need to return to the wide version of data. This new variable will be just a covariate that I will use in regression a&lt;SPAN style="font-family: inherit;"&gt;nalysis and I need to work with the wide format of the data.&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="font-family: inherit;"&gt;I found ways to concatenate a list of character or numeric variables. I wonder if there is a function that I use to calculate the sum of values for these years where my Year values are 1 (that is individuals were admitted to hospital).&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="font-family: inherit;"&gt;Thank you!&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 13 Dec 2020 23:17:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/For-each-individual-counting-how-many-variable-have-a-specific/m-p/705560#M216481</guid>
      <dc:creator>Emma_at_SAS</dc:creator>
      <dc:date>2020-12-13T23:17:56Z</dc:date>
    </item>
    <item>
      <title>Re: For each individual, counting how many variable have a specific value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/For-each-individual-counting-how-many-variable-have-a-specific/m-p/705562#M216482</link>
      <description>&lt;P&gt;In my opinion, covariates in regression analysis (or any modeling) are the exception to the rule where you should prefer long data sets to wide. I would still create the data set long, as Kurt Bremser has done, do as much analysis as you can for using the long data set, and then for regression use PROC TRANSPOSE to return it to wide.&lt;/P&gt;</description>
      <pubDate>Mon, 14 Dec 2020 00:24:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/For-each-individual-counting-how-many-variable-have-a-specific/m-p/705562#M216482</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-12-14T00:24:49Z</dc:date>
    </item>
    <item>
      <title>Re: For each individual, counting how many variable have a specific value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/For-each-individual-counting-how-many-variable-have-a-specific/m-p/705578#M216488</link>
      <description>Excellent! Thank you KurtBremser and PaigeMiller. Both your comments helped me to count the number of hospital admissions. How may I do similar steps for 2 sets of variables and create 2 new variables. I have another set of 7 variables for different infections and I want to count in total, how many infections each individual had. This is only for the last year's data (7 observations for each individual for the last year). &lt;BR /&gt;&lt;BR /&gt;I tried adding a second set of variables in the PROC TRANSPOSE step, and I saw the variables in the long format but I could not make the loop to count 1's in those 7 variables.&lt;BR /&gt;proc transpose data=have out=long (drop=_name_);&lt;BR /&gt;by id;&lt;BR /&gt;var y_:;&lt;BR /&gt;var infection_:;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;Thank you!</description>
      <pubDate>Mon, 14 Dec 2020 04:53:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/For-each-individual-counting-how-many-variable-have-a-specific/m-p/705578#M216488</guid>
      <dc:creator>Emma_at_SAS</dc:creator>
      <dc:date>2020-12-14T04:53:49Z</dc:date>
    </item>
    <item>
      <title>Re: For each individual, counting how many variable have a specific value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/For-each-individual-counting-how-many-variable-have-a-specific/m-p/706018#M216658</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
  select
    id,
    sum(case
      when col1 = 1 and upcase(_name_) like '%Y'
      then 1
      else 0
    end) as number_of_admissions_to_hospital
    sum(case
      when col1 = 1 and upcase(_name_) like '%INF'
      then 1
      else 0
    end) as number_of_infections
  from long
  group by id
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Add additional conditions as needed to the CASE clauses.&lt;/P&gt;</description>
      <pubDate>Tue, 15 Dec 2020 13:23:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/For-each-individual-counting-how-many-variable-have-a-specific/m-p/706018#M216658</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-12-15T13:23:20Z</dc:date>
    </item>
    <item>
      <title>Re: For each individual, counting how many variable have a specific value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/For-each-individual-counting-how-many-variable-have-a-specific/m-p/706031#M216667</link>
      <description>Thank you very much, KurtBremser!</description>
      <pubDate>Tue, 15 Dec 2020 14:22:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/For-each-individual-counting-how-many-variable-have-a-specific/m-p/706031#M216667</guid>
      <dc:creator>Emma_at_SAS</dc:creator>
      <dc:date>2020-12-15T14:22:36Z</dc:date>
    </item>
    <item>
      <title>Re: For each individual, counting how many variable have a specific value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/For-each-individual-counting-how-many-variable-have-a-specific/m-p/707317#M217157</link>
      <description>&lt;P&gt;Thank you,&amp;nbsp;KurtBremser. I cannot figure out what does the following part does. May you please help me with that?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE class="language-sas"&gt;&lt;CODE&gt;and upcase(_name_) like '%Y'&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;Thank you!&lt;/P&gt;</description>
      <pubDate>Sun, 20 Dec 2020 22:43:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/For-each-individual-counting-how-many-variable-have-a-specific/m-p/707317#M217157</guid>
      <dc:creator>Emma_at_SAS</dc:creator>
      <dc:date>2020-12-20T22:43:21Z</dc:date>
    </item>
    <item>
      <title>Re: For each individual, counting how many variable have a specific value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/For-each-individual-counting-how-many-variable-have-a-specific/m-p/707376#M217179</link>
      <description>&lt;P&gt;_NAME_ is a variable created by the TRANSPOSE procedure, it holds the name of the variable that was transposed to this particular observation.&lt;/P&gt;
&lt;P&gt;Here, I select for the values of all entries where the former variable name started with a Y, either upoer or lower case.&lt;/P&gt;</description>
      <pubDate>Mon, 21 Dec 2020 08:17:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/For-each-individual-counting-how-many-variable-have-a-specific/m-p/707376#M217179</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-12-21T08:17:50Z</dc:date>
    </item>
  </channel>
</rss>

