Desktop productivity for business analysts and programmers

Need quick solution on this condition

Accepted Solution Solved
Reply
Contributor
Posts: 74
Accepted Solution

Need quick solution on this condition

[ Edited ]

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


Accepted Solutions
Solution
‎03-08-2018 07:06 AM
Respected Advisor
Posts: 4,736

Re: Need quick solution on this condition

Posted in reply to Sourav_sas

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


All Replies
Contributor
Posts: 74

Re: Need quick solution on this condition

Posted in reply to Sourav_sas
Someone please look in to this issue.. Little bit urgent this one.
Super User
Posts: 10,278

Re: Need quick solution on this condition

Posted in reply to Sourav_sas

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.

 

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 74

Re: Need quick solution on this condition

Posted in reply to KurtBremser

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

Super User
Posts: 10,278

Re: Need quick solution on this condition

Posted in reply to Sourav_sas

Since value2 is numeric, you can't set it to a character value.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 74

Re: Need quick solution on this condition

Posted in reply to KurtBremser

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

 

 

Super User
Posts: 10,278

Re: Need quick solution on this condition

Posted in reply to Sourav_sas

That's a simple if - then statement, very easy to do. You won't need help with that.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Solution
‎03-08-2018 07:06 AM
Respected Advisor
Posts: 4,736

Re: Need quick solution on this condition

Posted in reply to Sourav_sas

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;
Contributor
Posts: 74

Re: Need quick solution on this condition


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.

 

Super User
Posts: 10,278

Re: Need quick solution on this condition

Posted in reply to Sourav_sas

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Highlighted
Contributor
Posts: 74

Re: Need quick solution on this condition

Posted in reply to KurtBremser

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

☑ This topic is solved.

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

Discussion stats
  • 10 replies
  • 232 views
  • 0 likes
  • 3 in conversation