Hi,
I want to extract initial one character from the character variables using an array and create two new variable (D, E).
My data look like this
data have1;
input id name$ gen$ PROCHI DATE A B C;
datalines;
1 m_ram male 1 1.1 1 . 1.23456
2 m_mohan male 1 1.1 99 1 1.23456
3 m_shyam male 2 2.1 1 99 1.23456
4 f_radha female 2 2.1 90 1 99
5 f_gita female 2 2.1 99 95 1.23456
6 f_sita female 2 3.1 . 1 1.23456
7 f_ranu female 3 2.1 1 . 1.23456
8 f_tina female 3 2.1 . 1 1.23456
;
run;
I want data like this
id name$ gen$ ID DATE A B C D E;
1 m_ram male 1 1.1 1 . 1.23456 m m
2 m_mohan male 1 1.1 99 1 1.23456 m m
3 m_shyam male 2 2.1 1 99 1.23456 m m
4 f_radha female 2 2.1 90 1 99 f f
5 f_gita female 2 2.1 99 95 1.23456 f f
6 f_sita female 2 3.1 . 1 1.23456 f f
7 f_ranu female 3 2.1 1 . 1.23456 f f
8 f_tina female 3 2.1 . 1 1.23456 f f
I have tried following code:
data want (drop=i) ;
set have1 ;
array try {2} $ name gen ;
array try2 {i} $ D E;
do i = 1 to 2;
try2 {i} = substr {try2{i},1,1} ;
end;
run;
But it's not working. It says 'undeclared array referenced' substr
Kindly suggest
Thanks in advance
Substr is a function, therefore the use of curly braces isn't going to work is it:
try2 {i} = substr {try{i},1,1};
Should it not be:
try2 {i} = substr(try{i},1,1);
You referenced the same array, the unpopulated one on both side of the =
try2 {i} = substr {try2{i},1,1}
use
try2 {i} = substr {try{i},1,1}
Substr is a function, therefore the use of curly braces isn't going to work is it:
try2 {i} = substr {try{i},1,1};
Should it not be:
try2 {i} = substr(try{i},1,1);
@RW9 wrote:
Substr is a function, therefore the use of curly braces isn't going to work is it:
try2 {i} = substr {try{i},1,1};Should it not be:
try2 {i} = substr(try{i},1,1);
Good catch, my monitor resolution makes it hard to see curly braces so I don't use them.
SUBSTR does not use curly brackets. Use parentheses instead. Note that the second array name should be TRY, not TRY2.
try2 {i} = substr (try{i},1,1) ;
Alternatively, get clever and get rid of SUBSTR:
array try2 {i} $1 D E;
do i = 1 to 2;
try2 {i} = try{i} ;
end;
Because the array defines the new variables as 1 character long, you only have room to store the first character.
The array and loop is a bit pointless unless learning arrays:
data want; set have; d=char(name,1);
e=char(gen,1);
run;
Thanks for the reply. This is really a good way to get rid of ARRAY
Thanks for such an apt solution
Thanks, this also worked perfectly!!!
Hi:
You could do all your manipulation in the original program. I don't actually see that using an ARRAY is necessary for what your stated purpose is. I also don't see the need to read ID and PROCHI with those names if you want PROCHI to be called ID in the final output. You might also use the SCAN function to get rid of the m_ or f_ in front of name (assuming that if you substring it out you want name without that string).
Hope this gives you some suggestions for a different approach.
Cynthia
Thanks, Cynthia for such an insightful reply.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.