Hi,
I'm looking to create a single data step where I use a repetitive IF statement for 24 columns (income_201001, income_201002, income_201003 ...... ,income_201012, income_201101,income_201102 ...... income_201112) . The first four numbers represent the year (i.e. take values 2010 or 2011) and the last two numbers represent months (1 to 12).
The IF statement I want to use is the following:
IF income_201001 > 0 then flag_1=1; ELSE flag_1=0;
IF income_201002 > 0 then flag_2=1; ELSE flag_2=0;
…
IF income_201111 > 0 then flag_23=1; ELSE flag_23=0;
IF income_201112 > 0 then flag_24=1; ELSE flag_24=0;
What's the quickest way to write this without having to repeat 24 lines of code?
Thanks!
That is what the ARRAY statement is for. Also boolean expressions evaluate as 0 or 1 already.
array income income_201001-income_201012 income_201101-income_201112 ;
array flag_ [24];
do index=1 to dim(income);
flag_[index] = income[index] > 0;
end;
That is what the ARRAY statement is for. Also boolean expressions evaluate as 0 or 1 already.
array income income_201001-income_201012 income_201101-income_201112 ;
array flag_ [24];
do index=1 to dim(income);
flag_[index] = income[index] > 0;
end;
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.
Ready to level-up your skills? Choose your own adventure.