BookmarkSubscribeRSS Feed
goliPSU
Calcite | Level 5

I have a table wchich looks like below:

year    elec    ngas    petro    transp

1993    0.328962596541413    0.0333333333333333    0.0294741106255071    0.0312981997615055

1994    0.32131918710743    0.0357852882703777    0.0280891133921987    0.0296270473156796

1995    0.312689346078104    0.0301204819277108    0.0299476941480979    0.0307595385921147

1996    0.302492954358166    0.0296846011131725    0.0306720643123235    0.0315941372524655

1997    0.297555088719812    0.037109375    0.0313930047898444    0.0324508531293975

1998    0.279078628962933    0.0390455531453362    0.0324409310705472    0.0333306670536662

1999    0.269449037043657    0.0390946502057613    0.0338084853707251    0.0342346177942214

2000    0.268587236213135    0.0389105058365759    0.0354842304721889    0.0367621612481076

2001    0.267152131952974    0.0426829268292683    0.0346861443472607    0.036119401007415

2002    0.25010602338106    0.041501976284585    0.0347417788989821    0.0361976486955247

2003    0.249007386555198    0.04    0.034782939573557    0.0362781339184914

I want to automatically generate the following vectors:

hhd_nrg_coeff_order = {'elec', 'ngas', 'petro', 'transp'};

hhd_nrg_coeffs_93 = {0.328962596541413 ,   0.0333333333333333,   0.0294741106255071 ,   0.0312981997615055};

hhd_nrg_coeffs_94 = {0.32131918710743 ,   0.0357852882703777,    0.0280891133921987,    0.0296270473156796};

....

hhd_nrg_coeffs_03 = {0.249007386555198    0.04    0.034782939573557    0.0362781339184914}

is there any way to generate it automatically in SAS.

because I have more data till 2010 so it is gana be nasty in the code.

Thanks

2 REPLIES 2
Rick_SAS
SAS Super FREQ

Sure, but I'd recommend using a matrix and nidexing the rows instead of creating separate row vectors, one for each row of the data.

Here's what I'd do (this code has not been run...might have syntax errors). To get the var names, use

use MyData;

read all var _NUM_ into X[colname=hhd_nrg_coeff_order];

close MyData;

That sets the vector hhd_nrg_coeff_order to {"year"..."transp"};

Then extract the first column and convert it to a character vector:

y = X[,1];

year = char(y);

X = X[,2:ncol(X)]; /* strip off first col */

hhd_nrg_coeff_order = hhd_nrg_coeff_order[,2:ncol(X)];

Now you can use a trick that I recently blogged about where you can assign the YEAR variable to be the row index.

The blog is Access rows or columns of a matrix by names - The DO Loop

It looks like this:

mattrib X[rowname=year];

You can now refer to each row by using the name of the row. For example:

DataFor1997 = X['1997', ];

If you need a loop, you can use

do i = 1 to nrow(year);

   z = X[year, ]; /* get i_th row */

   /* do stuff...*/

end;

Of course, you can also omit the MATTRIB statement and just index the rows by i, and use year in case you need the value of YEAR for that observation.

goliPSU
Calcite | Level 5

Thank you so very mcuh. It is a big help.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

Multiple Linear Regression in SAS

Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.

Find more tutorials on the SAS Users YouTube channel.

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 2 replies
  • 886 views
  • 1 like
  • 2 in conversation