SAS Looping

Hi,

I am new to SAS.

I have two datasets.

Dataset1:
Yr
2010
2011
2012

Dataset2:
Yr Value
2010 100
2010 200
2010 300
2011 150
2011 250
2011 350
2012 500
2012 550
2012 600

This is what I want to do:

For each Yr in dataset1,
 Run a proc for dataset2.Value
 where Dataset2.Yr = Dataset1.Yr;
End

Help will be greatly appreciated. Thanks!!

**Also, I cannot find SAS as a topic and have selected what seemed to me to be the best option available.
j1981Asked:
Who is Participating?
 
IanStatisticianCommented:
Typo  Should have used the word  JOIN  as set out below. (sorry).

    create table datasetNew as
    select    dataset2.Yr,
              dataset2.Value
    from
              dataset1U
    join
              dataset2
    on        dataset1U.Yr = dataset2.Yr
    order by
              dataset2.Yr;

Open in new window

0
 
IanStatisticianCommented:
Hi there J1981,

There are a few ways of skinning this cat.  If the order within years is does not necessarily have to be maintained because this method may re-arrange values within years, though you can specify that the values are sorted.  In the code below I use
*  <anything except semi colon> ;
as general comments, so that I can use
/*
    whole groups of lines including code with semicolons
*/
type comments when debugging.


****
First extract all records where the Yr variable in dataset2 
matchs one of the Yr values in dataset1.
Note that to protect against the proc sql producing multiple
lots of records for any year mentioned in dataset1 more than
once, we maks sure we only use unique years from dataset1.
****;

proc sql;

   *** just the unique records from dataset1  ***;
   create table dataset1U  as
   select   unique Yr
   from     dataset1;

   *** 
          "join" those years with dataset 2 so we end up with
           only those dataset2 records that are in dataset1
   ***;
    create table datasetNew as
    select    dataset2.Yr,
                  dataset2.Value
    from
                  dataset1U
    left
                  dataset2
    on         dataset1U.Yr = dataset2.Yr
    order by
                 dataset2.Yr;

   *** could have  "order by dataset2.Yr, dataset2.Value"  ***;
  
quit;

*** 
       process datasetNew using a "by" statement will most
       likely be sufficient for the required processing
***;
proc <your proc name> data=datasetnew;

by  Yr;    *** This is necessary  ***;

<your proc statements>;

run;
quit;

Open in new window

---------

For example proc means will work a treat in this context.

Ian
0
 
j1981Author Commented:
Very clear answer. Thank you! Also, as an aside, do you know if SAS is available as a topic, so that I can directly put it, instead of choosing 'miscellaneous databases' ?
0
 
IanStatisticianCommented:
Hi there again,

SAS is not a topic that I could find.   However there has been a regular number of SAS queries here, which are answered quite well by various members.

hope you pick up the methods of programming in SAS.


Ian
0
 
j1981Author Commented:
Ok, thank you!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.