BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
yashpande
Obsidian | Level 7
I have an input dataset as below

input_variables
if a
if b
if c

And another dataset with expression
expression
"""= 10 then pk=100;"
"""= 20 then pk=300;"
"""= 30 then pk=500;"
"""= 40 then pk=600;"
"""= 50 then pk=800;"
"""= 60 then pk=900;"
"""= 70 then pk=1000;"
"""= 80 then pk=1300;"
"""= 90 then pk=1500;"
"""= 100 then pk=1800;"




Now I want output as iteration of variable for all expression. Here is how it should look like

"if a""= 10 then pk=100;"
"if a""= 20 then pk=300;"
"if a""= 30 then pk=500;"
"if a""= 40 then pk=600;"
"if a""= 50 then pk=800;"
"if a""= 60 then pk=900;"
"if a""= 70 then pk=1000;"
"if a""= 80 then pk=1300;"
"if a""= 90 then pk=1500;"
"if a""= 100 then pk=1800;"
"if b""= 10 then pk=100;"
"if b""= 20 then pk=300;"
"if b""= 30 then pk=500;"
"if b""= 40 then pk=600;"
"if b""= 50 then pk=800;"
"if b""= 60 then pk=900;"
"if b""= 70 then pk=1000;"
"if b""= 80 then pk=1300;"
"if b""= 90 then pk=1500;"
"if b""= 100 then pk=1800;"
"if c""= 10 then pk=100;"
"if c""= 20 then pk=300;"
"if c""= 30 then pk=500;"
"if c""= 40 then pk=600;"
"if c""= 50 then pk=800;"
"if c""= 60 then pk=900;"
"if c""= 70 then pk=1000;"
"if c""= 80 then pk=1300;"
"if c""= 90 then pk=1500;"
"if c""= 100 then pk=1800;"



Any help is really appreciated
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Seems like a strange idea, but let's work through it so you can learn how this might work.

 

First let's be clearer about what we have.  I am assuming you have two datasets.  So let's present some code that will create those datasets.  Let's call them LEFT and RIGHT.

data left;
  input input_variables $50. ;
cards;
if a
if b
if c
;

data right;
  input expression $50. ;
cards4;
= 10 then pk=100;
= 20 then pk=300;
= 30 then pk=500;
= 40 then pk=600;
= 50 then pk=800;
= 60 then pk=900;
= 70 then pk=1000;
= 80 then pk=1300;
= 90 then pk=1500;
= 100 then pk=1800;
;;;;

The simplest way to get all combinations is to use PROC SQL to perform a Cartesian product of the two sets.

 

Now I think you are saying you want to concatenate the two variables into a new character variable.

proc sql;
create table want as
select a.input_variables
     , b.expression
     , catx(' ',a.input_variables,b.expression) as statement
from left a 
      , right b
;
quit;

So now you have a dataset with three variables.

 

What do you want do with this next? 

Do you want to somehow run those statements?  If so what data do you want to run them against?

 

Are you sure you want run all three of these statements in the same step? 

if a = 10 then pk=100;
if b = 10 then pk=100;
if c = 10 then pk=100;

If so the order you run them might determine what value PK ends up having.

 

So perhaps it would help a lot if you took a step back and explained the overall picture of what you are trying to do.  Perhaps treating code as data is not the easiest way to get from what you have to what you want.

View solution in original post

1 REPLY 1
Tom
Super User Tom
Super User

Seems like a strange idea, but let's work through it so you can learn how this might work.

 

First let's be clearer about what we have.  I am assuming you have two datasets.  So let's present some code that will create those datasets.  Let's call them LEFT and RIGHT.

data left;
  input input_variables $50. ;
cards;
if a
if b
if c
;

data right;
  input expression $50. ;
cards4;
= 10 then pk=100;
= 20 then pk=300;
= 30 then pk=500;
= 40 then pk=600;
= 50 then pk=800;
= 60 then pk=900;
= 70 then pk=1000;
= 80 then pk=1300;
= 90 then pk=1500;
= 100 then pk=1800;
;;;;

The simplest way to get all combinations is to use PROC SQL to perform a Cartesian product of the two sets.

 

Now I think you are saying you want to concatenate the two variables into a new character variable.

proc sql;
create table want as
select a.input_variables
     , b.expression
     , catx(' ',a.input_variables,b.expression) as statement
from left a 
      , right b
;
quit;

So now you have a dataset with three variables.

 

What do you want do with this next? 

Do you want to somehow run those statements?  If so what data do you want to run them against?

 

Are you sure you want run all three of these statements in the same step? 

if a = 10 then pk=100;
if b = 10 then pk=100;
if c = 10 then pk=100;

If so the order you run them might determine what value PK ends up having.

 

So perhaps it would help a lot if you took a step back and explained the overall picture of what you are trying to do.  Perhaps treating code as data is not the easiest way to get from what you have to what you want.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 330 views
  • 2 likes
  • 2 in conversation