-2

i would like to make an iterative loop in SAS to calculate the average for each id my data as follow :

data sample

3rdthemagical
  • 5,271
  • 18
  • 36
  • First of all sass, sas and ssas are widely different things. Then, on this site you are expected to provide sample data, desired output and show us with code what you already tried. The sample data is preferably given as text that can be copy-pasted rather than a link to a picture. – user2877959 Oct 04 '17 at 12:02

2 Answers2

0

In SAS data steps you use do loops for iterative processing https://blogs.sas.com/content/iml/2011/09/07/loops-in-sas.html.

However, to get an average value per idea, SQL is usually much simpler.

proc sql;
  select id, mean(value)
  from data
  group by id
  ;
quit;
user2877959
  • 1,792
  • 1
  • 9
  • 14
  • that was so helpful thank you. but iam still looking for do loops. – user2975180 Oct 04 '17 at 12:11
  • you are _"looking for do loops"_ in this context in particular? or in general? I don't see a reason to use do loops for solving your particular question (average per id). If you are looking for info on how loops work in SAS in general, just read the article I linked. – user2877959 Oct 04 '17 at 12:46
  • Thanks. in this question i provide a sample of data. i have thousand of records and i need to do some operation like sum and average of different records. that is why i ask about the loop. – user2975180 Oct 04 '17 at 12:52
  • can you help to answer this ? https://stackoverflow.com/questions/46579332/combine-different-sql-into-one-table-using-sas – user2975180 Oct 05 '17 at 10:20
  • did you post it? I had a look at it but the question is very poorly formulated. The post should be edited (a lot) to make it more clear, provide sample data, desired output, etc... – user2877959 Oct 05 '17 at 10:42
  • Yes, i did post it. i will provide data sample. all what i want is to create one table have all these queries. – user2975180 Oct 05 '17 at 10:50
  • https://stackoverflow.com/questions/46628955/count-the-values-in-one-column-by-sql-using-sas – user2975180 Oct 08 '17 at 10:23
0
data begin;
    input id value;
    cards;
    1 10 
    1 20 
    1 5 
    2 9
    2 6
    3 50
    3 20 
    3 44
    3 79
; run;

proc sort data=begin; by id; run; /*This is not needed in our case, but it is good practice to always sort in SAS.*/

proc means data=begin; 
    by id;  /*These are variables you count by*/
    var value; /*The value you count the mean by. You also get additional values. */
run;

Your can also replace var(mean) with mean(value)= my_mean

pinegulf
  • 1,334
  • 13
  • 32
  • Thank you, that was a cool thing. the process is too slow because I have thousands of records. – user2975180 Oct 04 '17 at 12:23
  • @user2975180 you can use **data=begin noprint;** and **output out = mean_sas_data** commands. This is useful for dataset too large for proc sql. – pinegulf Oct 04 '17 at 13:16