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.

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