- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi There,
anybody can help me how can i use the VNAME function sas datastep in array.
see the example below, what I am trying to do:
I am using varibale PHHXNSA
1) to create new vriable i.e. log of PHHXNSA
2) and I want to assign the variable name using PHHXNSA i.e. compress(vname(CrVar(i)) || '_LN') so that new varibale name will be PHHXNSA_LN
data QQ1;
set QQ;
array CrVar(1) PHHXNSA;
do i = 1 to 1;
NewV:smileyinfo: = compress(vname(CrVar(i)) || '_LN');
*PHHXNSA_LN = log(CrVar(i));
end;
run;
Thanks,
KP
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
I think you miss the point of arrays, they are designed to work over groups of variables of the same name with a numeric suffix, i.e. COL1-COLx.
If you want to create lots of variables with the same name as the base then do something like:
data _null_;
set sashelp.vcolumn (where=(libname="SASHELP" and memname="CLASSFIT" and name in ("Age","Height","Weight"))) end=last;
if _n_=1 then call execute('data work.classfit; set sashelp.classfit;');
call execute(' '||strip(name)||'_LN=log('||strip(name)||');');
if last then call execute('run;');
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
thank for the reply.
I do understand the use of array... but my proble is naming convetion i want to keep.
In my example there is only one varibale but it will be more.... means
suppose i have array of 3 varibale
array Var(3) V1 X1 Z1;
now i want to crate new variable which is Log(Var(i)) and want to assign the name to the new variable as V1_LN , X1_LN, Z1_LN and so on...
Thanks,
KP
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hence, you need to use the metadata of the dataset to indicate what variables you want to operate on. Arrays work like this:
array XYZ{3} a b c;
What this does is setup three variables XYZ1, XYZ2, XYZ3 - note the numeric suffix, then you can operate on these variables by using the suffix, or by using the array as an object. It does not in anyway use the metadata of the dataset itself, which is what you are trying to manipulate. If you wanted output like COL1-COL3 then you could use arrays:
array XYZ{3} a b c;
array COL{3} 5.;
COL{i}=log(XYZ{i});
But this would not name the variable the same as the base variable because there is no link to the underlying metadata. Or to put it another way, XYZ2 does not know its b, it just contains the value of b. So you need to know it and program to it.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thanks Guys
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Variables and their names are created by SAS during the compilation phase of the data step.
The VNAME function works during the execution phase, when variables have already been defined, and when additional variables cannot be defined.
So what you are looking for is quite probably a macro solution:
%macro logvar(varname);
&varname._ln = log(&varname);
%mend;
Now you can use that macro (%logvar()) on every variable in the data step you want to have "logged".
Keep in mind that the macro is executed to generate text that is handed to SAS for the data step compilation phase, long before the data step actually runs!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi there,
sorry may be i dont fully understand this.
how can i use this in array?
thanks,
KP
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
You can't use this in an array. The SAS array is a construct that helps you adress groups of variables DURING the data step execution. You want to define new variables, which is done BEFORE the data step executes.
What you want to do is generate code dynamically, which is exactly what the macro processor is made for.
If you need to dynamically create new variables, you need to somehow generate a list of variables to be created, and then use this to dynamically create the necessary data step code. How you best achieve this, depends on the data you already have and what you want to get.
RW9 has already given you a nice example how it can be done. Try to understand what his code actually does (USE the ONLINE MANUAL!!!), you may end up with an idea how to solve your own problem.