BookmarkSubscribeRSS Feed
rajbhinde
Calcite | Level 5

Hi! 

Let's assume I am creating dataset like this

 

 

input a b c;
datalines;
1 1 1
2 2 2

 

This works fine.

 

What am trying to create -

a = 1;
b = 2;
c = 3;
input a b c;
datalines;
a b c 
a b c

Basically, I want to create a dataset based on the values present inside a variable & this value is coming via a loop hence is different every time & cannot be hard coded.

 

Thanks in advance

11 REPLIES 11
PaigeMiller
Diamond | Level 26

@rajbhinde wrote:

 

What am trying to create -

a = 1;
b = 2;
c = 3;
input a b c;
datalines;
a b c 
a b c

Basically, I want to create a dataset based on the values present inside a variable & this value is coming via a loop hence is different every time & cannot be hard coded.

 


I don't understand what you want. Please show us what you want, a screen capture is fine for showing us what you want. If you are going to show us SAS code (as above), please show us complete SAS data steps, not partial data steps.

--
Paige Miller
rajbhinde
Calcite | Level 5

I have a set of variables that get their value based on an if else condition - 

 

if a = 1 then a_new = 10

else if a = 2 then a_new = 20

else a_new = 5

 

Like this, I have 35 variables that would go through if else & get their value

 

Now, what am trying to do is, create a Data Set for this "x_new" variable. Once the first row of values is inserted, in the second row, I want to hardcode one value, lets say a_new & then rest 34 variables getting their values from if else condition

I hope am clear with the requirement.

PaigeMiller
Diamond | Level 26

@rajbhinde wrote:

I have a set of variables that get their value based on an if else condition - 

 

if a = 1 then a_new = 10

else if a = 2 then a_new = 20

else a_new = 5

 

Like this, I have 35 variables that would go through if else & get their value

 

Now, what am trying to do is, create a Data Set for this "x_new" variable. Once the first row of values is inserted, in the second row, I want to hardcode one value, lets say a_new & then rest 34 variables getting their values from if else condition

I hope am clear with the requirement.


Sorry, no, I don't get it. Showing us what output you want would be a great help.

--
Paige Miller
rajbhinde
Calcite | Level 5

Let's say, I have 3 variables that get their value through an if-else statement. 

 

For example - a=1,b=2,c=3........these values would vary based upon if-else condition

 

creating a data set for these 3 variables should give an output like this - 

 

abc
123

 

Now what I want to do is, run a loop where in each loop am hardcoding the value of one variable, and the table should look something like this 

abc
1 ( as it is from if-else condition )2 ( as it is from if-else condition )3 ( as it is from if-else condition )
99 ( hardcoded )2 ( as it is from if-else condition )3 ( as it is from if-else condition )
1 ( as it is from if-else condition )85 (hardcoded)3 ( as it is from if-else condition )
1 ( as it is from if-else condition )2 ( as it is from if-else condition )33 (hardcoded)

 

Now, if you look back at my original post, am trying to achieve something like this -

 

Data Test;
input a b c; datalines; a b c 99 b c
a 85 c
a b 33
proc print data = test;
run;

 But datalines is not supporting variable values 

PaigeMiller
Diamond | Level 26

Hello, @rajbhinde 

 

I think this gets you to what you want. I think you have to read in the original data as character strings and then use the VVALUEX function, as you can't do this entirely with DATALINES

 

Data Test;
input var1 $  var2 $  var3 $;
datalines;
a b c 
99 b c
a 85 c
a b 33
;
data want;
a=1; b=2; c=3;
set test;
if anydigit(var1)^=0 then finalvar1=input(var1,10.);
else finalvar1=vvaluex(var1);
if anydigit(var2)^=0 then finalvar2=input(var2,10.);
else finalvar2=vvaluex(var2);
if anydigit(var3)^=0 then finalvar3=input(var3,10.);
else finalvar3=vvaluex(var3);
run;

 

--
Paige Miller
rajbhinde
Calcite | Level 5
Hi Paige,

Appreciate your time & help. Since I am relatively new to SAS, I would try to implement what you've suggested step by step & get back to you. Thanks!
Tom
Super User Tom
Super User

It is still as clear as mud, but it looks like perhaps you want to some type of code generation.

 

So you have this METADATA.

data metadata;
  input (var1-var3) (:$40.);
datalines;
a b c 
99 b c
a 85 c
a b 33
;

And you want to generate this code:

data want;
  set have;
  var1=a;
  var2=b;
  var3=c;
  output;
  var1=99;
  var2=b;
  var3=c;
  output;
....
run;

So just use a simple data step like this to generate that repetitive code from the metadata.

filename code temp;
data _null_;
  set metadata;
  file code ;
  put var1= ';' var2= ';' var3= ';output;' ;
run;

And then use it inside a data step by using %INCLUDE;

data have;
  input a b c ;
cards;
1 2 3
;
data want;
  set have;
%include code / source2;
run;

Log:

584  data want;
585    set have;
586  %include code / source2;
NOTE: %INCLUDE (level 1) file CODE is (system-specific pathname).
587 +var1=a ;var2=b ;var3=c ;output;
588 +var1=99 ;var2=b ;var3=c ;output;
589 +var1=a ;var2=85 ;var3=c ;output;
590 +var1=a ;var2=b ;var3=33 ;output;
NOTE: %INCLUDE (level 1) ending.
591  run;

NOTE: The data set WORK.WANT has 4 observations and 6 variables.
NOTE: DATA statement used (Total process time):
      real time           0.02 seconds
      cpu time            0.03 seconds

Results:

OBS    a    b    c    var1    var2    var3

 1     1    2    3      1       2       3
 2     1    2    3     99       2       3
 3     1    2    3      1      85       3
 4     1    2    3      1       2      33

rajbhinde
Calcite | Level 5
Hi Tom,

Appreciate your time & help. Since I am relatively new to SAS, I would get a good understanding of what you've suggested & get back to you. Thanks!
Tom
Super User Tom
Super User

@rajbhinde wrote:

I have a set of variables that get their value based on an if else condition - 

 

if a = 1 then a_new = 10

else if a = 2 then a_new = 20

else a_new = 5

 

Like this, I have 35 variables that would go through if else & get their value

 

Now, what am trying to do is, create a Data Set for this "x_new" variable. Once the first row of values is inserted, in the second row, I want to hardcode one value, lets say a_new & then rest 34 variables getting their values from if else condition

I hope am clear with the requirement.


If you want to recode 35 variables then perhaps you need to use ARRAYs?

Here is example using just three variables.  To extend to 35 just change the list of variables in the two array statements.

data want;
  set have;
  array old a b c ;
  array new a_new b_new c_new;
  do index=1 to dim(old);
    if old[index] = 1 then new[index] = 10
    else if old[index] = 2 then new[index] = 20
    else new[index] = 5
  end;
run;
AMSAS
SAS Super FREQ

Is this what you want to do?

data want ;
	do i=1 to 5 ;
		a=i ;
		b=i ;
		c=i ;
		output want ;
	end ;
run ;


If not then I suggest you provide a clear example of the dataset you want to create 

rajbhinde
Calcite | Level 5
Hi!

Appreciate you taking out time for this. Unfortunately, that's not what am looking for. Can you please check my reply to PaigeMillers comment to get a better understanding? Thanks!

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 11 replies
  • 1074 views
  • 0 likes
  • 4 in conversation