BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
cobba
Obsidian | Level 7

I have the following data. The real data is about 100 columns

 

data have;
    input id$ cause1$ cause2$ cause3$ effect1$ effect2$ effect3$ @@@@@@@;
    datalines;
    aa 001 001 002 001 003 .
    bb 005 . . 004 005 001
    cc 001 003 002 001 003 002
    dd 002 004 009 002 005 006
    ;
run;

The codes aren't very helpful for analysis. So I'd like to add a character to the start based on the header.

image.png

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

VNAME function within an array. 

 

data want;
set have;

array _vars(*) cause1--effect3;

do i=1 to dim(_vars);
     _vars(i) = catt(substr(vname(_vars), 1, 1), _vars(i));
end;

run;

@cobba wrote:

I have the following data. The real data is about 100 columns

 

data have;
    input id$ cause1$ cause2$ cause3$ effect1$ effect2$ effect3$ @@@@@@@;
    datalines;
    aa 001 001 002 001 003 .
    bb 005 . . 004 005 001
    cc 001 003 002 001 003 002
    dd 002 004 009 002 005 006
    ;
run;

The codes aren't very helpful for analysis. So I'd like to add a character to the start based on the header.

image.png

 

 


 

View solution in original post

4 REPLIES 4
Reeza
Super User

VNAME function within an array. 

 

data want;
set have;

array _vars(*) cause1--effect3;

do i=1 to dim(_vars);
     _vars(i) = catt(substr(vname(_vars), 1, 1), _vars(i));
end;

run;

@cobba wrote:

I have the following data. The real data is about 100 columns

 

data have;
    input id$ cause1$ cause2$ cause3$ effect1$ effect2$ effect3$ @@@@@@@;
    datalines;
    aa 001 001 002 001 003 .
    bb 005 . . 004 005 001
    cc 001 003 002 001 003 002
    dd 002 004 009 002 005 006
    ;
run;

The codes aren't very helpful for analysis. So I'd like to add a character to the start based on the header.

image.png

 

 


 

cobba
Obsidian | Level 7
I'm getting the following error when I run your code. I am running SAS EG 7.1 under SAS v9.3

ERROR: Array subscript out of range at line 39 column 30.
id=aa cause1=001 cause2=001 cause3=002 effect1=001 effect2=003 effect3= i=1 _ERROR_=1 _N_=1
Shmuel
Garnet | Level 18

Replace line:

 _vars(i) = catt(substr(vname(_vars), 1, 1), _vars(i));

into:

 _vars(i) = catt(substr(vname(_vars(i)), 1, 1), _vars(i));
Reeza
Super User

Replace the IF with this:

Accounts for missing and I had missed the (i) in the array reference within the VNAME() function. 

 

if not missing(_vars(i)) then _vars(i) = catt(substr(vname(_vars(i)), 1, 1), _vars(i));

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 686 views
  • 5 likes
  • 3 in conversation