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

Hi, I want to create a do loop to increment the values of a variable in a sas dataset.

 

I am writing the following code:

 

Proc sql;

select Off into: Off_Var separated by ','

from Control_Table;

quit;

 

%do i=0 to 1;

%let Off_Mac= &Off_Var.&i;

%end;

 

I want to put the first value from Off_Var to Off_Mac when i=0, and put the second value of Off_Var to Off_Mac when i=1.

 

Please help, 

Thanks 

1 ACCEPTED SOLUTION

Accepted Solutions
katariasarthak
Obsidian | Level 7

Hi,

 

This code worked for me!

I am not sure what %bquote does tho.

 

Thanks anyways 🙂

%do i=0 %to 1;
  %let Off_Mac= %scan(%bquote(&Off_Var),&i,%str(,));
%end;

 

View solution in original post

5 REPLIES 5
Kurt_Bremser
Super User

You will only get one macro variable out fo the SQL, so you need to dissect it:

%do i=0 %to 1;
  %let Off_Mac= %scan(&Off_Var,&i+1);
%end;

(assuming there's lots of more code in the macro loop, as you always end up with the second word after the loop)

katariasarthak
Obsidian | Level 7

Hi,

 

This code worked for me!

I am not sure what %bquote does tho.

 

Thanks anyways 🙂

%do i=0 %to 1;
  %let Off_Mac= %scan(%bquote(&Off_Var),&i,%str(,));
%end;

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

What is it your trying to do?  Really don't see the point in this.  I mean if you need to split that var into two then:

data _null_;
  set control_table;
  call symputx('off_mac1',scan(off,1,","));
  call symputx('off_mac2',scan(off,2,","));
run;

But that just seems to be a waste of coding time, if you already have the data in the dataset, you can simply merge that row onto the data you need to check and check then, hence avoiding all this messy code.  Providing test data in the form of a datastep and what you want to see at the end really helps clarify questions.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 5 replies
  • 862 views
  • 0 likes
  • 3 in conversation