BookmarkSubscribeRSS Feed
SASSLICK001
Obsidian | Level 7

HI all 

 

I have situation where I have 3 treatment variables. some times there might note be any data pertaining trt3 but I need to get the variable trt3 in the dataset. How can I achieve this?

It may be trt2 or trt1 also missing, so I need to get automatically the missing treatment columns dynamically!

6 REPLIES 6
kannand
Lapis Lazuli | Level 10

Hello, you can create an empty dataset with the 3 variables and set with your actual data so all your vars are available even if one is missing as in the example below:

 

data in1;
  infile datalines dlm='09'x;
  input trt1:$2.  trt2 $2. trt3 $2.;;
datalines;
2	12	15
;
run;
data in1; set in1(obs=0);RUN;
proc print;
  data in2;infile datalines dlm='09'x;
  input trt1:$2.  ;
datalines;
2	
;RUN;
data final;
     set in1 in2;
run;
proc print;
run;
Kannan Deivasigamani
kannand
Lapis Lazuli | Level 10

 If you're reading a flat file, as Tom mentioned in his post, the TRUNCOVER option can help. However, if you are bringing in a couple of SAS datasets and if you need to have a set of variables in your output dataset, then you're better off in creating an empty dataset and using it as a template along with the other datasets that you are reading in which may or may not have your required variables, so you know for sure that you will have those variables coming from the template  in your output. 

 

Hope this makes sense. 

 

Good Luck...!!!

Kannan Deivasigamani
Tom
Super User Tom
Super User

I think perhaps you meant something a little different than what you wrote.  For if you have three variables then it doesn't matter if the values are missing the variables are there.  But if you have one varaible with potentially three values then the fact that one of the potential values does not appear might be a concern.  

That is what the COMPLETETYPES and/or CLASSDATA options on PROC MEANS is designed to deal with.

Jagadishkatam
Amethyst | Level 16

I come across this situation while generating the counts by treatment, so here is the approach that i follow

 

libname sampsio list;

/*create a format for the missing treatment as well, here as per data treatment 5 is missing*/
proc format;
value drug
1='trt1'
2='trt2'
3='trt3'
4='trt4'
5='trt5';
run;

/*use the completetypes along with preloadfmt and format to apply the format to treatment so that while */
/*calculating frequency counts even for missing treatment count will be created*/
proc summary data=sampsio.drug n completetypes nway;
class drug / preloadfmt;
var chang_bp;
format drug drug.;
output out=freq n=count;
run;

/*finally transpose the data to see the missing treatment variable, here */
/*from the data treatment 5 was missing but it appears in the final dataset*/
proc transpose data=freq out=trt;
id drug;
var count;
run;

Thanks,

Jag

Thanks,
Jag
Jim_G
Pyrite | Level 9

I think what SASLICK is saying is that  trt1 or trt2 or trt3 is blank in some records. Such as.

 

id  trt1  trt2  trt3

1    45   22    21c

2    33   33

3   

4    7     9

 

and wants to input the record and set  trt1 trt2 trt3  to missing..

Tom
Super User Tom
Super User

That is what the TRUNCOVER option on the INFILE statement is for.

data want ;
  infile cards truncover ;
  input id  trt1-trt3 ;
cards;
1    45   22   21
2    33   33
3   
4    7     9
;;;;

 

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 6 replies
  • 1018 views
  • 2 likes
  • 5 in conversation