From: Kaz on 7 May 2010 04:27 Dear SAS experts, I want to execute the code that depending on the value of a variable, pick one variable from same dataset. To do this I used call symput but call symput seems to work only after the data step. Does anyone know how to capture the variable value into macro variable during data step executions? Please see the code below. data aaa; input index var500 var1000 var1500 var2000; cards; 500 1 10 100 1000 1000 1 10 100 1000 1500 1 10 100 1000 2000 1 10 100 1000 ; run; /* The following data step does not work but this is what I want */ data bbb; set aaa; /* Capture the value of index */ call symput('index_macro',left(trim(index))); /* Then value of a variable to new variable. e.g. if index=500 then var500 value is input in a_index */ a_index=var&index_macro; run; Thank you, Kaz from Japan
From: RolandRB on 7 May 2010 04:52 On 7 Mai, 10:27, Kaz <kazutoshi.shideh...(a)gmail.com> wrote: > Dear SAS experts, > > I want to execute the code that depending on the value of a variable, > pick one variable from same dataset. > To do this I used call symput but call symput seems to work only after > the data step. > Does anyone know how to capture the variable value into macro variable > during data step executions? > > Please see the code below. > > data aaa; > input index var500 var1000 var1500 var2000; > cards; > 500 1 10 100 1000 > 1000 1 10 100 1000 > 1500 1 10 100 1000 > 2000 1 10 100 1000 > ; > run; > > /* The following data step does not work but this is what I want */ > data bbb; > set aaa; > /* Capture the value of index */ > call symput('index_macro',left(trim(index))); > > /* Then value of a variable to new variable. e.g. if index=500 then > var500 value is input in a_index */ > a_index=var&index_macro; > run; > > Thank you, > Kaz from Japan You need to "symget()" the macro if you want to use it in a data step the way you are doing. symget() is the opposite of symput()
From: Kaz on 7 May 2010 05:36 Thanks but symget does not seem to work. The thing I want to achieve is that choose variable depending on the value of index. data bbb; set aaa; if index=500 then a_index=var500; else if index=1000 then a_index=var1000; else if index=1500 then a_index=var1500 else if index=2000 then a_index=var2000; run; as there are so many variables in my dataset, I want to simplify the code as follows; I am not sure how you can get value of varXXXX(XXXX depending on value of index). data bbb; set aaa; call symput('index_macro',left(trim(index))); a_index=var&index_macro; run; Thank you, Kaz
From: data _null_; on 7 May 2010 07:44 On May 7, 4:36 am, Kaz <kazutoshi.shideh...(a)gmail.com> wrote: > Thanks but symget does not seem to work. > The thing I want to achieve is that choose variable depending on the > value of index. > > data bbb; > set aaa; > if index=500 then a_index=var500; > else if index=1000 then a_index=var1000; > else if index=1500 then a_index=var1500 > else if index=2000 then a_index=var2000; > run; > > as there are so many variables in my dataset, I want to simplify the > code as follows; > I am not sure how you can get value of varXXXX(XXXX depending on value > of index). > > data bbb; > set aaa; > call symput('index_macro',left(trim(index))); > a_index=var&index_macro; > run; > > Thank you, > Kaz data aaa; input index var500 var1000 var1500 var2000; cards; 500 1 10 100 1000 1000 1 10 100 1000 1500 1 10 100 1000 2000 1 10 100 1000 ;;;; run; example1: data bbb; array v[2000] var1-var2000; set aaa; new = v[index]; drop var:; run; proc print; run; example2: proc transpose data = aaa out = ccc(where=(input(substr(_name_,4),f8.) eq index)) ; by index notsorted; var var:; run; proc print; run; example3: data ddd; set aaa; new = input(left(vvalueX(cats('var',index))),f8.); run; proc print; run;
From: Richard A. DeVenezia on 11 May 2010 16:45 On May 7, 4:27 am, Kaz <kazutoshi.shideh...(a)gmail.com> wrote: > Dear SAS experts, > > I want to execute the code that depending on the value of a variable, > pick one variable from same dataset. > To do this I used call symput but call symput seems to work only after > the data step. > Does anyone know how to capture the variable value into macro variable > during data step executions? > > Please see the code below. > > data aaa; > input index var500 var1000 var1500 var2000; > cards; > 500 1 10 100 1000 > 1000 1 10 100 1000 > 1500 1 10 100 1000 > 2000 1 10 100 1000 > ; > run; > > /* The following data step does not work but this is what I want */ > data bbb; > set aaa; > /* Capture the value of index */ > call symput('index_macro',left(trim(index))); > > /* Then value of a variable to new variable. e.g. if index=500 then > var500 value is input in a_index */ > a_index=var&index_macro; > run; > > Thank you, > Kaz from Japan As data _null_ has shown, VVALUEX will return the formatted value of a dynamically specified variable. Another way is to use a hash object to map the index value to an appropriate linear array index. Note that when you perform lots of programming to deal with the column names, then you could have to much data in the metadata and might find benefit in redesigning the table layout or performing a transposition. ---------- data aaa; input index var500 var1000 var1500 var2000; cards; 500 1 10 100 1000 1000 1 10 100 1000 1500 1 10 100 1000 2000 1 10 100 1000 ; run; data foo; set aaa; array vars var:; if _n_ = 1 then do; declare hash indexmap(); indexmap.defineKey('index'); indexmap.defineData('_index'); indexmap.defineDone(); do _n_ = 1 to dim (vars); _index = input (substr(vname(vars(_n_)),4),12.); indexmap.Add(key:_index, data:_n_); end; end; if (indexmap.find() = 0) then a_index = vars(_index); run; ---------- Richard A. DeVenezia http://www.devenezia.com
|
Pages: 1 Prev: Multiple files need processing Next: proc logistic with odds ratio in output table |