BookmarkSubscribeRSS Feed
KrunalPatel
Calcite | Level 5

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

7 REPLIES 7
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

KrunalPatel
Calcite | Level 5

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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

KrunalPatel
Calcite | Level 5

Thanks Guys

Kurt_Bremser
Super User

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!

KrunalPatel
Calcite | Level 5

Hi there,

sorry may be i dont fully understand this.

how can i use this in array?

thanks,

KP

Kurt_Bremser
Super User

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register 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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 2394 views
  • 0 likes
  • 3 in conversation