BookmarkSubscribeRSS Feed
MadhuGujjula
Calcite | Level 5

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.

 

Namevar1varNew_Name
Usa25Usa
Mad.4Usa
bad.4Usa
sad2.Usa
pad2.Usa
India.2India
Mad..India
bad9.India
sad..India
pad..India
Aus.0.1Aus
Mad..Aus
bad9.Aus
sad..Aus
pad.0.1Aus
8 REPLIES 8
Shmuel
Garnet | Level 18

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;
Astounding
PROC Star

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;
MadhuGujjula
Calcite | Level 5

I got nothing while running your code

 

Namevar1varnew_name
USA25 
Mad.4 
bad.4 
sad2. 
pad2. 
INDIA.2 
Mad.. 
bad9. 
sad.. 
pad.. 
AUS.0.1 
Mad.. 
bad9. 
sad.. 
pad.0.1 

 

 

ed_sas_member
Meteorite | Level 14

Hi @MadhuGujjula 

 

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;
ballardw
Super User

@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.

MadhuGujjula
Calcite | Level 5

I have tried %let New_name="USA" "INDIA" "AUS" but same result. Please help me out on this.

ed_sas_member
Meteorite | Level 14

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;

Capture d’écran 2020-03-06 à 12.21.47.png

satish_p
Calcite | Level 5

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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 8 replies
  • 845 views
  • 0 likes
  • 6 in conversation