<?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 to produce a dataset with addtional id in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-produce-a-dataset-with-addtional-id/m-p/665929#M199180</link>
    <description>&lt;P&gt;Dear mklangley,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for your continued attention and help. Today I pasted your codes into my SAS EG, it worked! I got output of four lines, each row is a unique sch_id. The new columns are group_color combined for the sum, This is exactly what I am looking for.&lt;/P&gt;&lt;P&gt;Thank you so much.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Next I will modify my codes according to my real world data structure and apply them to my dataset to see if I can get them out as desired.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you everyone who read and gave your help in many ways.&lt;/P&gt;&lt;P&gt;Best wishes, see you around.&lt;/P&gt;&lt;P&gt;Peter&lt;/P&gt;</description>
    <pubDate>Mon, 29 Jun 2020 21:48:22 GMT</pubDate>
    <dc:creator>Petergao1</dc:creator>
    <dc:date>2020-06-29T21:48:22Z</dc:date>
    <item>
      <title>How to produce a dataset with addtional id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-produce-a-dataset-with-addtional-id/m-p/665355#M198936</link>
      <description>&lt;PRE class="xisDoc-code"&gt;&lt;CODE&gt;Dear SAS users,&lt;BR /&gt;Below is the sample dataset and a program that will produce a one line output that include team name and color and each team's multiple game result. It works well.&lt;BR /&gt;&lt;BR /&gt;My question is suppose there are multiple entities (show up as a new id column such as schools id) and each entity will have the same set of groups, this is normal because schools may have the same set of groups and they will come to compete in some activities and got various result at different round of contests.&lt;BR /&gt;&lt;BR /&gt;The new dataset (call it team_new, the new column is sch_id) will be like this:&lt;BR /&gt;sch_id color        group        game1 game2 game 3   &lt;BR /&gt;sch1 Green          Crickets        10 7 8
sch1 Blue           Sea Otters      10 6 7
sch1 Yellow         Stingers        9 10 9
sch1 Red            Hot Ants        8 9 9
sch1 Purple         Cats            9 9 9
sch2 Green          Crickets        10 9 7
sch2 Blue           Sea Otters      8 7 9
sch2 Yellow         Stingers        7 8 10
sch2 Red            Hot Ants        9 8 10
sch2 Purple         Cats            8 6 9&lt;BR /&gt;sch3 Green          Crickets        5 7 9
sch3 Blue           Sea Otters      6 8 10
sch3 Yellow         Stingers        7 9 8
sch3 Red            Hot Ants        6 9 10
sch3 Purple         Cats            8 10 9&lt;BR /&gt;;&lt;BR /&gt;&lt;BR /&gt;The desired output will be like this:&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE class="xisDoc-code"&gt;&lt;CODE&gt;&lt;BR /&gt;sch_id      greencricketstotal blueseaottertotal yellowstingerstotal redhotantstotl purplecatstotal&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE class="xisDoc-code"&gt;&lt;CODE&gt;sch1 25 23 28 26 27&lt;BR /&gt;sch2 xx xx xx xx xx&lt;BR /&gt;sch3 xx xx xx xx xx&lt;BR /&gt;*note above XX will be the sum of three competition's result, not run yet.&lt;BR /&gt;&lt;BR /&gt;data teams; *This is the original sample dataset's name; input color $15. @16 team_name $15. @32 game1 game2 game3; datalines; Green Crickets 10 7 8 Blue Sea Otters 10 6 7 Yellow Stingers 9 10 9 Red Hot Ants 8 9 9 Purple Cats 9 9 9 ; %macro newvars(dsn); data _null_; set &amp;amp;dsn end=end; count+1; call symputx('macvar'||left(count),compress(color)||compress(team_name)||"Total"); if end then call symputx('max',count); run; data teamscores; set &amp;amp;dsn end=end; %do i = 1 %to &amp;amp;max; if _n_=&amp;amp;i then do; &amp;amp;&amp;amp;macvar&amp;amp;i=sum(of game1-game3); retain &amp;amp;&amp;amp;macvar&amp;amp;i; keep &amp;amp;&amp;amp;macvar&amp;amp;i; end; %end; if end then output; %mend newvars; %newvars(teams) proc print noobs; title "League Team Game Totals"; run; &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;The above program runs well. However, when I created the new dataset team_new with one more new column sch_id, and I tried to modify the above program, I can have it run, but only produce one line of output, which is the last color+group's record. I know I need to have more codes, but just cannot get it out the way I need. Any idea what I should do? By the way I would like to use the macro, with some modification, and do not want to do a transpose step. Thank you.&lt;/P&gt;</description>
      <pubDate>Fri, 26 Jun 2020 13:38:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-produce-a-dataset-with-addtional-id/m-p/665355#M198936</guid>
      <dc:creator>Petergao1</dc:creator>
      <dc:date>2020-06-26T13:38:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to produce a dataset with addtional id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-produce-a-dataset-with-addtional-id/m-p/665372#M198943</link>
      <description>&lt;P&gt;What is the purpose of the output data set? I does not appear to be well structured for most analysis tasks.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the use of the data is just to make a report that people read then you likely want a report procedure.&lt;/P&gt;
&lt;P&gt;Maybe something like:&lt;/P&gt;
&lt;PRE&gt;data teams; 
   input sch_id $ color $15. @16 team_name $15. @32 game1 game2 game3; 
   gametotal= sum(game1, game2, game3);
datalines;
sch1 Green          Crickets        10 7 8
sch1 Blue           Sea Otters      10 6 7
sch1 Yellow         Stingers        9 10 9
sch1 Red            Hot Ants        8 9 9
sch1 Purple         Cats            9 9 9
sch2 Green          Crickets        10 9 7
sch2 Blue           Sea Otters      8 7 9
sch2 Yellow         Stingers        7 8 10
sch2 Red            Hot Ants        9 8 10
sch2 Purple         Cats            8 6 9
sch3 Green          Crickets        5 7 9
sch3 Blue           Sea Otters      6 8 10
sch3 Yellow         Stingers        7 9 8
sch3 Red            Hot Ants        6 9 10
sch3 Purple         Cats            8 10 9
;


proc tabulate data=teams;
   class sch_id color team_name;
   var gametotal;
   table sch_id=' '*gametotal=' '* sum=' ',
         color=' '*team_name=' '
         / row=float
   ;
run;

          

&lt;/PRE&gt;
&lt;P&gt;And you original data would be better off with one record per game plus a variable to identify which game the score reflects. Then a report can be written that automatically adjusts to the number of games.&lt;/P&gt;
&lt;P&gt;Consider:&lt;/P&gt;
&lt;PRE&gt;data newteams;
   input sch_id $ color :$15. @16 team_name $15. @32 gamenumber score; 
datalines;
sch1   Green   Crickets        1 10 
sch1   Green   Crickets        2 7  
sch1   Green   Crickets        3 8  
sch1   Green   Crickets        4 6  
sch1   Blue    Sea Otters      1 10 
sch1   Blue    Sea Otters      2 6  
sch1   Blue    Sea Otters      3 7  
sch1   Blue    Sea Otters      4 3  
sch1   Yellow  Stingers        1 9  
sch1   Yellow  Stingers        2 10 
sch1   Yellow  Stingers        3 9  
sch1   Yellow  Stingers        4 7  
sch1   Red     Hot Ants        1 8  
sch1   Red     Hot Ants        2 9  
sch1   Red     Hot Ants        3 9  
sch1   Purple  Cats            1 9  
sch1   Purple  Cats            2 9  
sch1   Purple  Cats            3 9  
sch2   Green   Crickets        1 10 
sch2   Green   Crickets        2 9  
sch2   Green   Crickets        3 7  
sch2   Blue    Sea Otters      1 8  
sch2   Blue    Sea Otters      2 7  
sch2   Blue    Sea Otters      3 9  
sch2   Yellow  Stingers        1 7  
sch2   Yellow  Stingers        2 8  
sch2   Yellow  Stingers        3 10 
sch2   Red     Hot Ants        1 9  
sch2   Red     Hot Ants        2 8  
sch2   Red     Hot Ants        3 10 
sch2   Purple  Cats            1 8  
sch2   Purple  Cats            2 6  
sch2   Purple  Cats            3 9  
sch3   Green   Crickets        1 5  
sch3   Green   Crickets        2 7  
sch3   Green   Crickets        3 9  
sch3   Blue    Sea Otters      1 6  
sch3   Blue    Sea Otters      2 8  
sch3   Blue    Sea Otters      3 10 
sch3   Yellow  Stingers        1 7  
sch3   Yellow  Stingers        2 9  
sch3   Yellow  Stingers        3 8  
sch3   Red     Hot Ants        1 6  
sch3   Red     Hot Ants        2 9  
sch3   Red     Hot Ants        3 10 
sch3   Purple  Cats            1 8  
sch3   Purple  Cats            2 10 
sch3   Purple  Cats            3 9  

 


;


proc tabulate data=newteams;
   class sch_id color team_name;
   var score;
   table sch_id=' '*score=' '* sum=' ',
         color=' '*team_name=' '
         / row=float
   ;
run;&lt;/PRE&gt;
&lt;P&gt;If you look at the data you will see that several of the teams have different numbers of games but we still get an overall total without having to do anything different for different numbers of scores. And a minor change you can get different measures&lt;/P&gt;
&lt;PRE&gt;
proc tabulate data=newteams;
   class sch_id color team_name;
   var score;
   table sch_id=' '*score=' '* (sum='Total score ' mean='Average score'),
         color=' '*team_name=' '
         / row=float
   ;
run;&lt;/PRE&gt;</description>
      <pubDate>Fri, 26 Jun 2020 14:33:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-produce-a-dataset-with-addtional-id/m-p/665372#M198943</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-06-26T14:33:12Z</dc:date>
    </item>
    <item>
      <title>Re: How to produce a dataset with addtional id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-produce-a-dataset-with-addtional-id/m-p/665396#M198962</link>
      <description>&lt;P&gt;Since you said, "&lt;SPAN&gt;I would like to use the macro, with some modification, and do not want to do a transpose step," here is a solution that just requires a small edit to your macro. Instead of just outputting at the END of the dataset, it will output at the end of each distinct&amp;nbsp;&lt;STRONG&gt;sch_id&amp;nbsp;&lt;/STRONG&gt;group.&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro newvars(dsn); 
    data _null_;
        set &amp;amp;dsn end=end;
        count+1;
        call symputx('macvar'||left(count),compress(color)||compress(team_name)||"Total");
        if end then call symputx('max',count);
    run;

    data teamscores;
        set &amp;amp;dsn;
        by sch_id;
        %do i = 1 %to &amp;amp;max;
            if _n_=&amp;amp;i then do;
                &amp;amp;&amp;amp;macvar&amp;amp;i=sum(of game1-game3);
                retain &amp;amp;&amp;amp;macvar&amp;amp;i;
                keep sch_id &amp;amp;&amp;amp;macvar&amp;amp;i;
            end;
        %end;
        if last.sch_id then output;
    run;
%mend newvars;
%newvars(teams)&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 26 Jun 2020 16:03:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-produce-a-dataset-with-addtional-id/m-p/665396#M198962</guid>
      <dc:creator>mklangley</dc:creator>
      <dc:date>2020-06-26T16:03:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to produce a dataset with addtional id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-produce-a-dataset-with-addtional-id/m-p/665882#M199163</link>
      <description>&lt;P&gt;Hi, Ballardw,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The purpose of this request is to enhance the efficiency.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have datasets similar to this structure, that is sch_id, subgroup (has 10 rows) and annual performance data (say PI1, PI2, PI3).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I m looking for a solution to quickly process the data to have each school has its data of group_year_PI columns by each school_ID. The Macro should save me a lot of other data steps.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hope this help.&lt;/P&gt;</description>
      <pubDate>Mon, 29 Jun 2020 18:14:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-produce-a-dataset-with-addtional-id/m-p/665882#M199163</guid>
      <dc:creator>Petergao1</dc:creator>
      <dc:date>2020-06-29T18:14:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to produce a dataset with addtional id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-produce-a-dataset-with-addtional-id/m-p/665910#M199170</link>
      <description>&lt;P&gt;HI mklangley,&lt;/P&gt;&lt;P&gt;Thank you for taking your time reading my questions and making out a test program. I run your program. It worked. However, it still produce just one line, which is the very last record's sch_id, instead of three lines for three sch_id. (previously I did some trials and always have a output of one line of the last record in the input data set.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I was wondering it has to do with the sch_id column, somehow, I cannot get it done properly.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you. I will keep trying in finding a way out...&lt;/P&gt;</description>
      <pubDate>Mon, 29 Jun 2020 20:29:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-produce-a-dataset-with-addtional-id/m-p/665910#M199170</guid>
      <dc:creator>Petergao1</dc:creator>
      <dc:date>2020-06-29T20:29:00Z</dc:date>
    </item>
    <item>
      <title>Re: How to produce a dataset with addtional id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-produce-a-dataset-with-addtional-id/m-p/665913#M199172</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data teams; 
   input sch_id $ color $15. @16 team_name $15. @32 game1 game2 game3; 
   gametotal= sum(game1, game2, game3);
datalines;
sch1 Green          Crickets        10 7 8
sch1 Blue           Sea Otters      10 6 7
sch1 Yellow         Stingers        9 10 9
sch1 Red            Hot Ants        8 9 9
sch1 Purple         Cats            9 9 9
sch2 Green          Crickets        10 9 7
sch2 Blue           Sea Otters      8 7 9
sch2 Yellow         Stingers        7 8 10
sch2 Red            Hot Ants        9 8 10
sch2 Purple         Cats            8 6 9
sch3 Green          Crickets        5 7 9
sch3 Blue           Sea Otters      6 8 10
sch3 Yellow         Stingers        7 9 8
sch3 Red            Hot Ants        6 9 10
sch3 Purple         Cats            8 10 9
;
run;

proc means data=teams nway missing noprint;
class sch_id color team_name;
var game:;
output out=sum1 sum=;
run;

data sum1;
length colorteam $32.;
set sum1;
colorteam = cats(strip(compress(Color)),strip(compress(team_name)));
run;

proc transpose data=sum1 out=sum2;
by sch_id;
id colorteam;
var gametotal;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Check if this works for you&lt;/P&gt;</description>
      <pubDate>Mon, 29 Jun 2020 20:59:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-produce-a-dataset-with-addtional-id/m-p/665913#M199172</guid>
      <dc:creator>smantha</dc:creator>
      <dc:date>2020-06-29T20:59:53Z</dc:date>
    </item>
    <item>
      <title>Re: How to produce a dataset with addtional id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-produce-a-dataset-with-addtional-id/m-p/665914#M199173</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/335171"&gt;@Petergao1&lt;/a&gt;&amp;nbsp;Actually, if you run the code below, you should see it produce three rows in the output dataset--one for each&amp;nbsp;&lt;STRONG&gt;sch_id&lt;/STRONG&gt;. Are you getting a different result? If so, please post the full code/log that you're are running.&lt;/P&gt;
&lt;PRE&gt;data teams;
    input sch_id $ color $ team_name &amp;amp; $10. game1 game2 game3;
    datalines;
sch1 Green          Crickets        10 7 8
sch1 Blue           Sea Otters      10 6 7
sch1 Yellow         Stingers        9 10 9
sch1 Red            Hot Ants        8 9 9
sch1 Purple         Cats            9 9 9
sch2 Green          Crickets        10 9 7
sch2 Blue           Sea Otters      8 7 9
sch2 Yellow         Stingers        7 8 10
sch2 Red            Hot Ants        9 8 10
sch2 Purple         Cats            8 6 9
sch3 Green          Crickets        5 7 9
sch3 Blue           Sea Otters      6 8 10
sch3 Yellow         Stingers        7 9 8
sch3 Red            Hot Ants        6 9 10
sch3 Purple         Cats            8 10 9
;
run;

%macro newvars(dsn); %macro _; %mend _;
    data _null_;
        set &amp;amp;dsn end=end;
        count+1;
        call symputx('macvar'||left(count),compress(color)||compress(team_name)||"Total");
        if end then call symputx('max',count);
    run;

    data teamscores;
        set &amp;amp;dsn;
        by sch_id;
        %do i = 1 %to &amp;amp;max;
            if _n_=&amp;amp;i then do;
                &amp;amp;&amp;amp;macvar&amp;amp;i=sum(of game1-game3);
                retain &amp;amp;&amp;amp;macvar&amp;amp;i;
                keep sch_id &amp;amp;&amp;amp;macvar&amp;amp;i;
            end;
        %end;
        if last.sch_id then output;
    run;
%mend newvars;
%newvars(teams);

proc print; run;&lt;/PRE&gt;
&lt;P&gt;My output:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="mklangley_0-1593464380361.png" style="width: 553px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/46778i096842676D486528/image-dimensions/553x83?v=v2" width="553" height="83" role="button" title="mklangley_0-1593464380361.png" alt="mklangley_0-1593464380361.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 29 Jun 2020 21:00:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-produce-a-dataset-with-addtional-id/m-p/665914#M199173</guid>
      <dc:creator>mklangley</dc:creator>
      <dc:date>2020-06-29T21:00:46Z</dc:date>
    </item>
    <item>
      <title>Re: How to produce a dataset with addtional id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-produce-a-dataset-with-addtional-id/m-p/665929#M199180</link>
      <description>&lt;P&gt;Dear mklangley,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for your continued attention and help. Today I pasted your codes into my SAS EG, it worked! I got output of four lines, each row is a unique sch_id. The new columns are group_color combined for the sum, This is exactly what I am looking for.&lt;/P&gt;&lt;P&gt;Thank you so much.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Next I will modify my codes according to my real world data structure and apply them to my dataset to see if I can get them out as desired.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you everyone who read and gave your help in many ways.&lt;/P&gt;&lt;P&gt;Best wishes, see you around.&lt;/P&gt;&lt;P&gt;Peter&lt;/P&gt;</description>
      <pubDate>Mon, 29 Jun 2020 21:48:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-produce-a-dataset-with-addtional-id/m-p/665929#M199180</guid>
      <dc:creator>Petergao1</dc:creator>
      <dc:date>2020-06-29T21:48:22Z</dc:date>
    </item>
  </channel>
</rss>

