BookmarkSubscribeRSS Feed
iamthejvd
Calcite | Level 5

Doing an array and I want the formula in the array to be a lookup, something like this:

 

data table.population;
set table.population;

 

Array income_(n)  income_1 - income _100;

Array age_(n) age_1-age_100;

 

Do n=1 to 100;

                 if n=1 then age_(n) = current_age;

                 else age_(n) = age_(n-1)+1;

end;

 

Do n=1 to 100;

                income_(n) = lookup(age_(n), table.age_by_income, Income);

end;

 

the lookup would be somthing like a vlookup in excel, returning the income based on the age array.

5 REPLIES 5
stat_sas
Ammonite | Level 13

Please provide a sample data and desired output.

ballardw
Super User

I think you are looking for the WHICHN (or WHICHC if using character variables).

 

Whichn returns the position in an array that matches a specified value.

For instance

 

x = whichn(23, of age_(*)); Would return the which element of the array Age_ has the value of 23 or a 0 if not found.

 

So if I wanted to find an associated value I might use

 

y = Income (whichn(23,of age_(*))); to find the value in the income array that is in the same position as 23 in the age array.

 

I have no idea what you meand by table.age_by_Income. If you are intending to use a different dataset then that will not work at all in the manner you think. It may be possible in Proc Sql.

You may need to provide a more concrete example, with values for variables of your input and expected output. If you want code tested provide the data in the form of a datastep that reads a datalines or cards data section.

FreelanceReinh
Jade | Level 19

Hi @iamthejvd,

 

Most probably, you can define a numeric informat, say INC_AGE, from the table "age_by_income" and then define income_(n) like this:

income_(n) = input(put(age_(n), 3.), inc_age.);

If table "age_by_income" contains age groups (such as "20-24", "25-30" etc.), format 3. in the above code will be replaced by a user-defined format. For more specific advice we would need to know what "age_by_income" looks like.

iamthejvd
Calcite | Level 5

example data and calculation in excel attached.

FreelanceReinh
Jade | Level 19

Thanks for providing the data. You can create the informat I mentioned with the following code:

data inc_age;
infile cards eof=last;
retain fmtname '@inc_age';
input start label;
output;
return;
last: 
  hlo='O';
  label=.;
  output;
cards; 
30 7.50
31 13.53
32 8.66
33 11.13
34 14.46
35 15.73
36 13.01
37 16.24
38 6.78
39 12.30
40 16.26
41 14.46
42 15.73
43 13.01
;

proc format cntlin=inc_age;
run;

Then you can use the assignment statement from my previous post without any change:

income_(n) = input(put(age_(n), 3.), inc_age.);

Please make sure to fix your ARRAY statements by deleting the "(n)".

 

Catch up on SAS Innovate 2026

Dive into keynotes, announcements and breakthroughs on demand.

Explore Now →
How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 5931 views
  • 0 likes
  • 4 in conversation