BookmarkSubscribeRSS Feed
roshnigupta1602
Calcite | Level 5

i want to transpose the dataset with the condition.

 

if there is no observation available in dataset only variable is present then no transpose .

and if observation is present then transpose the dataset.

 

Please help me in this.

Thanks in advance. 

3 REPLIES 3
Kurt_Bremser
Super User

Query DICTIONARY.TABLES, then execute code conditionally:

%let count = 0;

proc sql noprint;
select nobs into :count
from dictionary.tables
where libname = "YOURLIB" and memname = "YOURDATASET";
quit;

% if &count. gt 0
%then %do;
  /* transpose code */
%end;
PaigeMiller
Diamond | Level 26

I think you would have to do this conditional transpose with a macro, or with a macro %IF statement. In this example, I am looking for the number of observations in SASHELP.CLASS. Naturally you would replace SASHELP and CLASS with the actual libname and data set name. (Note: if the data set is in the WORK library, you have to use libname="WORK" in your SQL WHERE clause).

 

%macro cond_transp;
    proc sql;
         select nobs into :nobs from dictionary.tables where libname="SASHELP" and memname="CLASS";
    quit;
    %if &nobs>0 %then %do;
        proc transpose data= ...;
            /* Other proc transpose statements go here */
         run;
    %end;
%mend;
%cond_transp

 

--
Paige Miller
Kurt_Bremser
Super User

Note that, with a reasonably recent maintenance level of SAS 9.4, you can use %IF %THEN %DO - %END in open code, so you do not need a macro definition.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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