DATA Step, Macro, Functions and more

need to create new variables using the one variable

Accepted Solution Solved
Reply
Contributor
Posts: 68
Accepted Solution

need to create new variables using the one variable

Hi experts,

 

I have one variable named as Country and it has values below


India
Unites States of America
United Arab Emirates

 

Need to create diffrent variables using these values example var1-india var2 United var3-States Var4-of var5-America etc....

 

and this needs to be dynamic ,( not with hardcoded)  Please advice


Accepted Solutions
Solution
‎09-08-2017 06:04 AM
Super User
Posts: 6,542

Re: need to create new variables using the one variable

Posted in reply to ambadi007

Whatever you are intending to do with this data, there are probably better ways.  But here is one way to get what you ask:

 

data each_word;

set have;

if country  >  ' ' then do _n_= 1 to countw(country);

   word = scan(country, _n_);

   total_words + 1;

   output;

end;

run;

proc transpose data=each_word out=want (drop=_name_) prefix=var;

   var word;

   id total_words;

run;

View solution in original post


All Replies
PROC Star
Posts: 1,190

Re: need to create new variables using the one variable

Posted in reply to ambadi007

Quick question: Why? Smiley Happy

Super User
Super User
Posts: 9,211

Re: need to create new variables using the one variable

Posted in reply to ambadi007

Second quick question, what is the question?  Post example test data in the form of a datastep using a code window (its the {i}) and what the output should look like.   Then explain any logic between.  If it is just splitting a string up into component parts then:

data want;
  set have;
  array var{5} $200;
  do i=1 to countw(your_string," ");
    var{i}=scan(your_string,i," ");
  end;
run;
Contributor
Posts: 68

Re: need to create new variables using the one variable

Hi , This was  my need , thanks

Solution
‎09-08-2017 06:04 AM
Super User
Posts: 6,542

Re: need to create new variables using the one variable

Posted in reply to ambadi007

Whatever you are intending to do with this data, there are probably better ways.  But here is one way to get what you ask:

 

data each_word;

set have;

if country  >  ' ' then do _n_= 1 to countw(country);

   word = scan(country, _n_);

   total_words + 1;

   output;

end;

run;

proc transpose data=each_word out=want (drop=_name_) prefix=var;

   var word;

   id total_words;

run;

☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 136 views
  • 1 like
  • 4 in conversation