DATA Step, Macro, Functions and more

Identically named vriables in sas dataset

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 124
Accepted Solution

Identically named vriables in sas dataset

Hi,

I am working with 3 sas datasets each of which have 2 identically named variables e.g txtcmmnt. One of them has a label and the other doesn't. When I use a keep option it only shows one variable but a proc contents or a veiw of the datasets it shows the 2 variables. I need to stack the 3 datasets into a final sas dataset . I would like to delete the variable with no label. How should I do it? Any suggestions will be helpful.

Thanks!


Accepted Solutions
Solution
‎11-11-2015 12:49 PM
Trusted Advisor
Posts: 1,137

Re: Identically named vriables in sas dataset

I am not sure if we could have 2 variables with the same name in the same dataset. Could you please check again, i think they will be some difference in names of those 2 variables.
Thanks,
Jag

View solution in original post


All Replies
Solution
‎11-11-2015 12:49 PM
Trusted Advisor
Posts: 1,137

Re: Identically named vriables in sas dataset

I am not sure if we could have 2 variables with the same name in the same dataset. Could you please check again, i think they will be some difference in names of those 2 variables.
Thanks,
Jag
Frequent Contributor
Posts: 124

Re: Identically named vriables in sas dataset

Posted in reply to Jagadishkatam

I thought the same so I did a proc content to carefully check the spellings:

This is an example of what shows up in the proc contents:

Variable     Type    Length    Format    Informat    Label

Txtcmmnt   Char    1754                                       1. Is there anything you would like to add?

Txtcmmnt   Char    146                       $146    

Its the second one with no label that I would like to delete.

Any suggestions?

Super User
Super User
Posts: 7,942

Re: Identically named vriables in sas dataset

As  has metioned, you cannot have two separate columns, which have the same name in one dataset.  Its not possible within SAS.  Can you provide example data?

If you stack data, then it should keep the properties of the first dataset:

data tmp;
  label a="Hello";
  a="Abc";
run;
data tmp2;
  a="Def";
run;
data final;
  set tmp tmp2;
run;

One way you could force it is to use SQL, with union all, and specify the labels in the first select:

proc sql;  
  create table FINAL as
  select  VARA label="xyz"
  from    TMP
  union all
  select * 
  from    TMP2
  union all...
quit;
Frequent Contributor
Posts: 124

Re: Identically named vriables in sas dataset

I agree with both of you. I checked the spellings of the variables . There is uppercase 'I' and looked to see if it is a '1' and it is not. The thing is when I try to stack the datasets , inspite of getting the lengths uniform on the variable txtcmmnt it gives me a warning message:

WARNING: Multiple lengths were specified for the variable txtCmmnt by input data set(s). This can cause

         truncation of data

The new lengths get applied only to the variable with the label. Have you come across something like this? Also, @RW9, the dataset looks like this

ID txtcmmnt txtcmmnt
1 eerrr  
2 rrrrr  
3 ttttt r
Super User
Super User
Posts: 7,942

Re: Identically named vriables in sas dataset

Try opening the properties of the two datasets.  What you are showing below doesn't really show anything.  I suspect the two variables have the same LABEL, but the variable NAME is different.  Run a proc contents of each one, or right click on the file and select properties.

Frequent Contributor
Posts: 124

Re: Identically named vriables in sas dataset

I figured it . The upper case 'I' was the lower case 'l' (L). I dropped the variable and it works fine. Thanks!

Trusted Advisor
Posts: 2,115

Re: Identically named vriables in sas dataset

dr2014: Your question would have been answered much quicker if you had included the ACTUAL variable names. There is no 'l' or '1' in the examples.
🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 274 views
  • 2 likes
  • 4 in conversation