Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Further usage of proc sql into (print varname) SAS

Tags:

sas

I used the sashelp library data sets as an example

proc datasets library=sashelp;
    contents
        data=company
        out=work.var_names;
quit;
run;

Now i using proc sql into to store the variable names

proc sql noprint;
    select name into: varname separated by ' '
    from var_names;
quit;
%put &varname;

Now i would like to print the variables name on the log

data newdata (drop=i);
    array temp{*} &varname;
    do i=1 to dim(temp);
        put temp{i};
    end;
run;

While it print . instead of the variable names.

Thanks

log:

337  %put &varname;
DEPTHEAD JOB1 LEVEL1 LEVEL2 LEVEL3 LEVEL4 LEVEL5 N

While i want

DEPTHEAD 
JOB1 
LEVEL1 
LEVEL2 
LEVEL3 
LEVEL4 
LEVEL5 
N
like image 775
useR Avatar asked Dec 05 '25 13:12

useR


2 Answers

Suppose the variables in the company are name, salary, the varname macro variable is "name salary". When the last segments were run, it is acually like this:

array temp{*} name salary;

The name and salary are set to default numeric variables with missing value '.'.

Why not just put the variables in the var_names:

data _null_;
    set var_names;
    put name;
run;

I don't really understand the premise of this question, as you're doing something waaaaay more complicated than you need to.

First of all, you don't need proc contents.

proc sql noprint;
    select name into :varname separated by ' '
    from dictionary.columns
    where libname='SASHELP' and memname='COMPANY';
quit;

Second, this is a much easier way to get the variable names, even assuming you don't want them as %put.

data _null_;
  set sashelp.company;
  array _names &varname.;
  do _i = 1 to dim(_names);
    _v = vname(_names[_i]);
    put _v=;
  end;
run;

I don't see what value a _temporary_ array has here at all. vname will happily give you the names of the variables in the array, if that's what you're after. Honestly this seems like a contrived example though as Reese and others pointed out.

like image 34
Joe Avatar answered Dec 07 '25 13:12

Joe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!