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