<?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 do I keep adding macro variables in dataset? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-keep-adding-macro-variables-in-dataset/m-p/479858#M123921</link>
    <description>&lt;P&gt;The problem is you are running multiple data steps and each one is starting from the same input data.&amp;nbsp; So the last one overwrites the previous one.&amp;nbsp; Make your macro only generate statements instead of steps. THen call it inside of data step to have it generate those statements all on one data step.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro data(var);
&amp;amp;var._mean=mean(of &amp;amp;var._:);
%mend data;

data student_stat;
  set student;
  %data(bmi)
  %data(energy)
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 20 Jul 2018 12:44:55 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2018-07-20T12:44:55Z</dc:date>
    <item>
      <title>How do I keep adding macro variables in dataset?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-keep-adding-macro-variables-in-dataset/m-p/479801#M123892</link>
      <description>&lt;P&gt;In this dataset, I have body mass index and total energy intake measurements for college students.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to calculate the mean body mass index and energy intake for each student using macro in SAS.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Using the code below&amp;nbsp;only replaces the mean values with the last variable.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there a way to keep adding the variables in macro?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Ideally, I want the final dataset to have mean body mass index as well as mean energy intake for each student.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This will be useful when I collect more data such as blood pressure...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks.&lt;/P&gt;&lt;PRE&gt;&lt;BR /&gt;data student;&lt;BR /&gt;input person_id bmi_1st energy_1st bmi_2nd energy_2nd ;&lt;BR /&gt;datalines;&lt;BR /&gt;1001 25 2000 23 1900&lt;BR /&gt;1002 21 1992 25 2190&lt;BR /&gt;1003 23 2123 25 3215&lt;BR /&gt;1004 29 2322 28 1555&lt;BR /&gt;1005 25 2222 23 1985&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;%macro data(var);&lt;BR /&gt;data student_stat; set student;&lt;BR /&gt;&amp;amp;var._mean=(&amp;amp;var._1st+&amp;amp;var._2nd)/2;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;%mend (data);&lt;BR /&gt;%data(bmi)&lt;BR /&gt;%data(energy)&lt;BR /&gt;run;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 20 Jul 2018 05:47:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-keep-adding-macro-variables-in-dataset/m-p/479801#M123892</guid>
      <dc:creator>sasworker16</dc:creator>
      <dc:date>2018-07-20T05:47:06Z</dc:date>
    </item>
    <item>
      <title>Re: keep macro variable in dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-keep-adding-macro-variables-in-dataset/m-p/479804#M123893</link>
      <description>&lt;P&gt;Transpose your dataset and use proc means/summary.&lt;/P&gt;
&lt;P&gt;You only need to add new variables in the var statement. No need for a macro.&lt;/P&gt;</description>
      <pubDate>Fri, 20 Jul 2018 05:48:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-keep-adding-macro-variables-in-dataset/m-p/479804#M123893</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-07-20T05:48:04Z</dc:date>
    </item>
    <item>
      <title>Re: How do I keep adding macro variables in dataset?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-keep-adding-macro-variables-in-dataset/m-p/479812#M123895</link>
      <description>&lt;P&gt;Just to expand on my previous post:&lt;/P&gt;
&lt;P&gt;Transpose to a helpful long dataset format:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data student;
input person_id bmi_1st energy_1st bmi_2nd energy_2nd ;
datalines;
1001 25 2000 23 1900
1002 21 1992 25 2190
1003 23 2123 25 3215
1004 29 2322 28 1555
1005 25 2222 23 1985
;
run;

proc transpose data=student out=int1;
by person_id;
var bmi: energy:;
run;

data int2;
set int1;
varname = scan(_name_,1,'_');
event = input(substr(scan(_name_,2,'_'),1,anyalpha(scan(_name_,2,'_'))-1),best.);
drop _name_;
run;

proc sort data=int2;
by person_id event;
run;

proc transpose data=int2 out=have (drop=_name_);
by person_id event;
var col1;
id varname;
run;

proc print data=have noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You now have a nice longitudinal dataset:&lt;/P&gt;
&lt;PRE&gt;person_
   id      event    bmi    energy

  1001       1       25     2000 
  1001       2       23     1900 
  1002       1       21     1992 
  1002       2       25     2190 
  1003       1       23     2123 
  1003       2       25     3215 
  1004       1       29     2322 
  1004       2       28     1555 
  1005       1       25     2222 
  1005       2       23     1985 
&lt;/PRE&gt;
&lt;P&gt;that lends itself nicely to analysis:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=have mean;
by person_id;
var bmi energy;
output out=want mean()=;
run;

proc print data=want noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;person_
   id      _TYPE_    _FREQ_     bmi    energy

  1001        0         2      24.0    1950.0
  1002        0         2      23.0    2091.0
  1003        0         2      24.0    2669.0
  1004        0         2      28.5    1938.5
  1005        0         2      24.0    2103.5
&lt;/PRE&gt;
&lt;P&gt;Bottom line: don't store data (event sequence) in structure (variable names). See Maxims 19 and 33.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 20 Jul 2018 06:33:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-keep-adding-macro-variables-in-dataset/m-p/479812#M123895</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-07-20T06:33:52Z</dc:date>
    </item>
    <item>
      <title>Re: How do I keep adding macro variables in dataset?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-keep-adding-macro-variables-in-dataset/m-p/479858#M123921</link>
      <description>&lt;P&gt;The problem is you are running multiple data steps and each one is starting from the same input data.&amp;nbsp; So the last one overwrites the previous one.&amp;nbsp; Make your macro only generate statements instead of steps. THen call it inside of data step to have it generate those statements all on one data step.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro data(var);
&amp;amp;var._mean=mean(of &amp;amp;var._:);
%mend data;

data student_stat;
  set student;
  %data(bmi)
  %data(energy)
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 20 Jul 2018 12:44:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-keep-adding-macro-variables-in-dataset/m-p/479858#M123921</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-07-20T12:44:55Z</dc:date>
    </item>
  </channel>
</rss>

