data have;
input Name $ var1 var;
datalines;
Usa 2 5
Mad . 4
bad . 4
sad 2 .
pad 2 .
India . 2
Mad . .
bad 9 .
sad . .
pad . .
Aus . .1
Mad . .
bad 9 .
sad . .
pad . .1
;
have like this. wanted like below.
Name | var1 | var | New_Name |
Usa | 2 | 5 | Usa |
Mad | . | 4 | Usa |
bad | . | 4 | Usa |
sad | 2 | . | Usa |
pad | 2 | . | Usa |
India | . | 2 | India |
Mad | . | . | India |
bad | 9 | . | India |
sad | . | . | India |
pad | . | . | India |
Aus | . | 0.1 | Aus |
Mad | . | . | Aus |
bad | 9 | . | Aus |
sad | . | . | Aus |
pad | . | 0.1 | Aus |
You need either a list or a dataset containing the new-name values.
Next is a code using a given list of new_names:
%let new_names = USA INDIA AUS ;
data want;
set have;
retain new_name;
if index(name,"&new_names") > 0
then new_name = name;
run;
When the list of values is in upper case, the program has to be changed slightly to account for that:
%let new_names = USA INDIA AUS ;
data want;
set have;
retain new_name;
if index(upcase(name),"&new_names") > 0
then new_name = name;
run;
Alternatively, you an switch to the FINDW function which supports modifiers such as "i" to ignore the case of the strings:
%let new_names = USA INDIA AUS ;
data want;
set have;
retain new_name;
if findw(name,"&new_names", , 'i') > 0
then new_name = name;
run;
I got nothing while running your code
Name | var1 | var | new_name |
USA | 2 | 5 | |
Mad | . | 4 | |
bad | . | 4 | |
sad | 2 | . | |
pad | 2 | . | |
INDIA | . | 2 | |
Mad | . | . | |
bad | 9 | . | |
sad | . | . | |
pad | . | . | |
AUS | . | 0.1 | |
Mad | . | . | |
bad | 9 | . | |
sad | . | . | |
pad | . | 0.1 |
Please try this:
%let new_names = "USA" "INDIA" "AUS" ;
data want;
set have;
retain new_name;
if upcase(name) in (&new_names)
then new_name = name;
run;
@MadhuGujjula wrote:
I got nothing while running your code
Run the code, go to the LOG, copy the code and any messages and notes from the log, open a code box on this forum by clicking on the {I} icon. Then Paste the copied text.
We have no idea what code you actually ran. So if you made a typo resulting in a data error then we can't tell what happened. if you referenced a different data set then you described, we can't tell. If created output from a different data set than was created, we can't tell.
I have tried %let New_name="USA" "INDIA" "AUS" but same result. Please help me out on this.
Very strange
This is what I get when I run the code:
data have;
input Name $ var1 var;
datalines;
Usa 2 5
Mad . 4
bad . 4
sad 2 .
pad 2 .
India . 2
Mad . .
bad 9 .
sad . .
pad . .
Aus . .1
Mad . .
bad 9 .
sad . .
pad . .1
;
%let new_names = "USA" "INDIA" "AUS" ;
data want;
set have;
retain new_name;
if upcase(name) in (&new_names)
then new_name = name;
run;
data have;
input Name $ var1 var;
datalines;
Usa 2 5
Mad . 4
bad . 4
sad 2 .
pad 2 .
India . 2
Mad . .
bad 9 .
sad . .
pad . .
Aus . .1
Mad . .
bad 9 .
sad . .
pad . .1
;
proc print data=have;
run;
data _null_;
file 'D:\king.txt';
put @5 'NEW_names';
put ' ';
run;
data _null_;
file 'D:\king.txt' mod;
if _n_=1 then do;
do Usa=1 to 5;
put @5 'Usa';
end;
end;
if _n_=1 then do;
do India=1 to 5;
put @5 'India';
end;
end;
if _n_=1 then do;
do Aus=1 to 5;
put @5 'Aus';
end;
end;
run;
data d;
infile 'D:\king.txt' firstobs=2;
input new_names $;
run;
proc print data=d;
run;
/*concatination*/
data fin;
merge have d;
run;
proc print data=fin;
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.