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.....

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 505 views
  • 2 likes
  • 3 in conversation