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

if I have 1 observation from a proc sql query how do i assign that to a variable? that i want to load into a variable 

 

proc sql outobs=1;
SELECT "Qa Test Id"n FROM ROBM.QUAL102;
run

 

so that further on I can do the below sort if the &var (QA Test ID)  from the select above is 35 for instance

 

 

if &var = 35 then do;
 proc sort data=ROBM.QUAL102;
  by TERM_CODE "Id"n CRN;
quit;
end;
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

I think you are looking to create a macro variable.  If so there is no need for the OUTOBS setting since unless you tell it to generate multiple variables or make a delimited list it will only write out one value into the macro variable.

proc sql noprint;
  SELECT "Qa Test Id"n 
    into :mvar
    FROM ROBM.QUAL102
  ;
quit;

You can then test the macro variable, but to conditionally generate code you will need to wrap the code into a macro o ruse some other method to generate the code. IF/THEN statements only work in DATA STEPS or some procs that support data step like data processing code.

%macro xxx ;
%if (&mvar = 35) %then %do;
proc sort data=ROBM.QUAL102;
  by TERM_CODE "Id"n CRN;
run;
%end;
%mend xxx;
%xxx;

 

View solution in original post

3 REPLIES 3
Reeza
Super User

You need to create a macro variable not a outobs=1 or create a dataset. Your current code will only output the data to the result window.

 

 

http://support.sas.com/documentation/cdl/en/mcrolref/69726/HTML/default/viewer.htm#n1y2jszlvs4hugn14...

 

proc sql noprint;
select max(age) into :max_age
from sashelp.class;
quit;

%put &max_age;
Tom
Super User Tom
Super User

I think you are looking to create a macro variable.  If so there is no need for the OUTOBS setting since unless you tell it to generate multiple variables or make a delimited list it will only write out one value into the macro variable.

proc sql noprint;
  SELECT "Qa Test Id"n 
    into :mvar
    FROM ROBM.QUAL102
  ;
quit;

You can then test the macro variable, but to conditionally generate code you will need to wrap the code into a macro o ruse some other method to generate the code. IF/THEN statements only work in DATA STEPS or some procs that support data step like data processing code.

%macro xxx ;
%if (&mvar = 35) %then %do;
proc sort data=ROBM.QUAL102;
  by TERM_CODE "Id"n CRN;
run;
%end;
%mend xxx;
%xxx;

 

robm
Quartz | Level 8

that works great thanks

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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
  • 2023 views
  • 0 likes
  • 3 in conversation