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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
andreas_lds
Jade | Level 19

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;

View solution in original post

3 REPLIES 3
Kurt_Bremser
Super User

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;
andreas_lds
Jade | Level 19

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;
Kumar6
Obsidian | Level 7

Thank You for your time.....

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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
  • 3 replies
  • 1240 views
  • 2 likes
  • 3 in conversation