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.....
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.