By using the below code I am trying to create 2 records. First record should be "JG" and other has to be "VC", but I only get "JG" in 2 records.
Can you help me how to create this.
data sub1;
subj = "201";
armcd = "JGVC";
run;
%let k =1;
data sub2;
set sub1;
armlen = length(armcd);
do i = 1 to armlen/2;
length split_armcd $200;
split_armcd = substr(armcd,&k.,2);
split_armcdn = i;
%let k=%eval(&k + 2);
output;
end;
run;
Thanks,
Kumar.
Why are you using macro-variables? Both %let-statements are evaluated even before the data-step is compiled, so the second %let has no effect on the code of the data-step at all.
data sub2;
set sub1;
length split_armcd $200;
k = 1;
armlen=lengthn(armcd);
do i=1 to armlen/2;
split_armcd=substr(armcd, k, 2);
split_armcdn=i;
k = k + 2;
output;
end;
run;
Macro statements and variables are resolved while the data step code is fetched for compilation, so the %eval will not have any effect on your data step code.
Do this instead:
data sub2;
set sub1;
length split_armcd $2;
do i = 1 to length(armlen) by 2;
split_armcd = substr(armcd,i,2);
split_armcdn = i / 2;
output;
end;
run;
(untested, posted from my tablet)
Simplified and tested code:
data sub2;
set sub1;
length split_armcd $2;
do split_armcdn = 1 to length(armcd) / 2;
split_armcd = substr(armcd,split_armcdn * 2 - 1,2);
output;
end;
run;
Why are you using macro-variables? Both %let-statements are evaluated even before the data-step is compiled, so the second %let has no effect on the code of the data-step at all.
data sub2;
set sub1;
length split_armcd $200;
k = 1;
armlen=lengthn(armcd);
do i=1 to armlen/2;
split_armcd=substr(armcd, k, 2);
split_armcdn=i;
k = k + 2;
output;
end;
run;
Thank You for your time.....
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.