0

Data set have has some columns with prefix Dex. But I don't know how many columns exactly with that prefix.

I want to create an array with values equal to those columns.

data want;
set have;
array Dex[100];
for i = 1 to 100;
[assign values]
end;
run;

Is there a way to do this without knowing those columns' names?

1
  • Your question is unclear. Arrays in SAS are variable references so they will contain the values of the variable already. Are you trying to re-assign the variable values, change them, or do something else? Commented May 12, 2015 at 14:42

2 Answers 2

2

Yes, you could define your array such as:

array vars Dex:;
do i=1 to dim(vars);
[assign values]
end;
run;
Sign up to request clarification or add additional context in comments.

3 Comments

N.B. this will fail if your dataset contains any character variables whose names also begin with DEX. NEOmen's answer could be adapted to avoid this.
@user667489. All variables with prefix Dex are based on the same type, either numeric or char, so it could be defined as an array. Even Omen's answer was also based on this assumption.
I could just add where type=char and it will work, but not sure if OP is looking for that!!
0

Use dictionary.columns to find out the number of columns which has prefix - dex

/Sample dataset/

 data have;
    dex_random1=1;
    dex_1=2;
    dex_3=4;
    dex_dex_random=2;
  run;

    proc sql;
    select count(*) into: Number_of_vars from dictionary.columns where 
    upcase(libname)="WORK" and upcase(memname)="HAVE" and upcase(name) like "DEX%";
    select name into: All_vars separated by " " from dictionary.columns where 
    upcase(libname)="WORK" and upcase(memname)="HAVE" and upcase(name) like "DEX%";
    quit;

    data want(drop=i);
    set have;
    array dex[&Number_of_vars.] &All_vars.  ;
    do i=1 to &Number_of_vars.;
    dex[i]=1;
    end;
    run;

2 Comments

This creates variables dex1, dex2, dex3, ... if those variables didn't exist already. I think OP probably wants existing dex% variables in the array.
@DWal ...above updated code takes care of this as well

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.