BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Sourav_sas
Quartz | Level 8

Suppose I have this following table

 

table.PNG

 

Here Name is character variable and rest of the two (Value1 and Value2) are numeric variable. I have one condition where Value1 is "Ult" then value2 should be "Ult" also, else the value of Value2 will be as it is... Can anyone give me one quick solution?.. Very urgent.. Regards Sourav

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

If you want tested code as answer then you should post the sample data as a working data step creating such data.

 

As for your question something as below should do (not tested as no data provided):

if value1='Ult' then value2=value1;

View solution in original post

10 REPLIES 10
Sourav_sas
Quartz | Level 8
Someone please look in to this issue.. Little bit urgent this one.
Kurt_Bremser
Super User

@Sourav_sas wrote:

Suppose I have this following table

 

table.PNG

 

Here Name is character variable and rest of the two (Value1 and Value2) are numeric variable. I have one condition where Value1 is "Ult" then value2 should be "Ult" also, else the value of Value2 will be as it is... Can anyone give me one quick solution?.. Very urgent.. Regards Sourav


Can't be. 'Ult' and 'Valt' are strings, and the values are left-aligned, which is the norm for character variables.

Please post example data in a data step with datalines, setting the correct types and formats.

Or use the macro provided in https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... to convert your dataset to a data step.

 

Sourav_sas
Quartz | Level 8

Hi Kurt

 

Thank for your reply. I know the situation is kind of this right now. But there may be some way, I can fulfill the condition as I mentioned, or I can not do this?

 

Regards

Sourav

Sourav_sas
Quartz | Level 8

Capture.PNG

I got your point. If the table looks like the above table, and only for Value1 Ult we need Value2 Ult, rest of the Value2 will be same, then what would we follow, can suggest me for that?

 

Regards

Sourav

 

 

Patrick
Opal | Level 21

If you want tested code as answer then you should post the sample data as a working data step creating such data.

 

As for your question something as below should do (not tested as no data provided):

if value1='Ult' then value2=value1;
Sourav_sas
Quartz | Level 8

@Patrick wrote:

If you want tested code as answer then you should post the sample data as a working data step creating such data.

 

As for your question something as below should do (not tested as no data provided):

if value1='Ult' then value2=value1;


I know this is very easy code, but my concern is in there from numeric to character,

 

I am using the following sample code.

Data Upload;
	Input Name $ 1-2 Value1 $ 3-7 Value2;
	
	Input (Value2, $4.);
	Datalines;
	A Ult   1
	B Valt  3
	C Ult   2
	D Valt  5
	;
	Run;

	Data Upload3;
		Set Upload;
Value5 = Put (Value2, 1.);
Value6 = Input (Value5, $1.);
Drop Value2 Value5;
Run;



	Data Upload2;
		Set Upload;
		If Value1 = 'Ult' then Value6 = Value1;
		Run;

Here I can convert the numeric to character value. In the last data step some how I am losing the value. The output is looking like as following screenshot.

 

Capture_New.JPG

 

I know this is very easy solution. Can I have that? As I am not able to do that.

 

Kurt_Bremser
Super User

You only set value6 when the condition is met, in other cases it stays missing. Add an "else" branch to your if statement that deals with those other cases.

Sourav_sas
Quartz | Level 8

Thanks guys for your help,,,,,, I am using this sort of sample code now, I am getting my output as expected....

Data Upload;
	Input Name $ 1-2 Value1 $ 3-7 Value2;
	Datalines;
	A Ult   1
	B Valt  3
	C Ult   2
	D Valt  5
	;
	Run;

	Data Upload;
		Set Upload;
Value5 = Put (Value2, 5. -l);
Value6 = Input (Value5, $10.);
If Value1 = 'Ult' then Value6 = Value1;
Else Value6 = Value5;
Drop Value2 Value5;
Run;

Output looks like....

 

 

Output.JPG

 

Thank you, thanks a lot for your support...

 

 

Regards

Sourav

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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
  • 10 replies
  • 1020 views
  • 0 likes
  • 3 in conversation