Hi,
I am trying to output records for each record with available avisit by using do loop as below. But I am getting below erro, please let me know what I am missing.
%let var1=1;
%let var2=2;
%let var3=3;
%let var4=4;
%let var5=5;
%let var6=6;
%let num=6;
data adsl_vis;
set adsl(keep=usubjid subjid);
do i=1 to #
avisitn=&&var&i;
output;
end;
run;
Thanks,
Chinna
Macro variables resolve before the data step starts to execute.
So please describe what you want in a bit more detail. This macro approach is cumbersome and placing many values into macro variables is quite often not a good idea in general.
You are using &i, which expects a macro variable named I to exist. The macro processor does not see data step variable values. If you must in a data step the function is SYMGET. (Please don't use data set's not defined )
data adsl_vis; /* set adsl(keep=usubjid subjid);*/ do i=1 to # avisitn=symget('var'||left(i)); output; end; run;
However than would create 6 output records for each one read, is that the desire?
However if the purpose of this is to create a sequence number then perhaps:
data adsl_vis; set adsl(keep=usubjid subjid); avisitn=_n_; run;
Macro variables resolve before the data step starts to execute.
So please describe what you want in a bit more detail. This macro approach is cumbersome and placing many values into macro variables is quite often not a good idea in general.
You are using &i, which expects a macro variable named I to exist. The macro processor does not see data step variable values. If you must in a data step the function is SYMGET. (Please don't use data set's not defined )
data adsl_vis; /* set adsl(keep=usubjid subjid);*/ do i=1 to # avisitn=symget('var'||left(i)); output; end; run;
However than would create 6 output records for each one read, is that the desire?
However if the purpose of this is to create a sequence number then perhaps:
data adsl_vis; set adsl(keep=usubjid subjid); avisitn=_n_; run;
@chinna0369 wrote:
By the way the variable which we are creating should be numeric AVISITN.
avisitn=input(symget('var'||left(i)),8.);
Basic use of INPUT to guarantee numeric result (and a hint why macro variables may not be a good approach)
If you want to add the same set of avisitn values to each record in the data set:
data avisitn_data; input avisitn; datalines; 1 2 3 4 5 6 ; Proc sql create table adsl_vis as select a.usubjid,a.subjid ,b.avisitn from adsl as a,avisitn_data as b ; quit;
The Proc SQL JOINS two data sets in a Cartesian Join, meaning every observation in one set is combined with every observation in a different data set.
IF however the data set Adsl already has values of Avisitn and you want to select only matching ones:
Proc sql create table want as select a.* from adsl as a right join avisitn_data a b on a.avisitn=b.avisitn ; quit;
Note that in neither case do you have to create multiple macro variables, attempt to use the sometimes flaky macro indirect reference or even count the number of values involved. Just place the list into the data set and Proc Sql will do the work.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.