BookmarkSubscribeRSS Feed
knveraraju91
Barite | Level 11

Dear,

 

I need help in which function i can in this situation.

 

my data has values           SS-S1 SSS >= 1    and SS-S1 SSS < 1. 

 

I need to create another variable with values                SSS >= 1 and  SSS < 1.

 

input data;

SS-S1 SSS >= 1

SS-S2 SSS < 1

 

 

output needed

SSS >= 1

SSS < 1

 

Please suggest. Thank you

 

 

7 REPLIES 7
PaigeMiller
Diamond | Level 26

As always, when such small and limited examples are presented, they are hard to generalize.

 

Is the rule that we are supposed to follow that SSS >=1 and SSS < 1 always begin in the 7th column? Or can the SSS >= 1 and SSS < 1 be anywhere in the text string? Do spaces matter — is it always SSS >= 1 or can it be SSS>=1 with no spaces? Does SS-SS1 and SS-SS2 matter at all?


What is the rule that you want programmed here?

 

--
Paige Miller
knveraraju91
Barite | Level 11

HI

 

Thank you for reply. I need second part of the value including spaces. I just need to remove first part of the value ' SS-S1'. Thank you

 

Vikram.V

PaigeMiller
Diamond | Level 26

What about when it is SS-S2? Are there other possible values?

 

You didn't really explain the rule you want programmed ... do we always start in column 7? Or can these codes that you are looking for be anywhere?

--
Paige Miller
knveraraju91
Barite | Level 11

Hi

 

Thank you very much

 There are only two values for the variable. I need value from column 7. If i use  want=substr(a,7);   i am getting the output i need.   Is there other option. Thank you

 

 

output needed;

SSS >= 1

SSS < 1

 

data one;
input a $1-14 ;
datalines;
SS-S1 SSS >= 1 
SS-S1 SSS < 1  
;
PaigeMiller
Diamond | Level 26

Yes, there are probably other ways to extract a string beginning in column 7, but I would stick with the SUBSTR() function.

--
Paige Miller
ballardw
Super User

@knveraraju91 wrote:

Hi

 

Thank you very much

 There are only two values for the variable. I need value from column 7. If i use  want=substr(a,7);   i am getting the output i need.   Is there other option. Thank you

 

 

output needed;

SSS >= 1

SSS < 1

 

data one;
input a $1-14 ;
datalines;
SS-S1 SSS >= 1 
SS-S1 SSS < 1  
;

I agree with @PaigeMiller that if substr is working for you then that may be best.

 

This depends on how you are getting the data into SAS in the first place but you may be able to READ that value with an input statement. Actual content and structure of your source may allow use of @"value" column input:

 

data one;
input @"SS-S1 " a $8. ;
datalines;
SS-S1 SSS >= 1 
SS-S1 SSS < 1  
;

On the input statement an option like @"SS-S1 " tells SAS to advance the read point to the column after the end of the value in quotes and then read the variable.

Concerns: The entire structure of the data and options use. If you are using list input then you need something that reads the spaces if the delimiter is not something other than that a space if the data is delimited. The approach I show will include any character immediately after the 1 in the "SSS < 1" case because we are forcing a read of 8 characters.

 

Another approach could be to use a custom Informat that would read SS-S1 SSS >= 1 into a value of "SSS >= 1". But again we have to either know the file contents when read or use a more complex method than substring:

proc format library=work;
invalue $sss
"SS-S1 SSS >= 1" = "SSS >= 1"
"SS-S1 SSS < 1"  = "SSS < 1"
;

data one;
input a $1-14 ;
datalines;
SS-S1 SSS >= 1 
SS-S1 SSS < 1  
;

data two;
   set one;
   newvar = input(a,$sss.);
run;

This approach might be preferred over SUBSTR if you have many values that need many different places to parse from.

 

Or just create a format and use the format without actually changing the value. This of course depends on what you are doing with the variable. If it is used for grouping in reports, analysis or graphs that should suffice.

 

proc format library=work;
value $sss
"SS-S1 SSS >= 1" = "SSS >= 1"
"SS-S1 SSS < 1"  = "SSS < 1"
;
run;

proc print data=one;
   format a $sss.;
run;

 

PaigeMiller
Diamond | Level 26

@ballardw wrote:

@knveraraju91 wrote:

Hi

 

Thank you very much

 There are only two values for the variable. I need value from column 7. If i use  want=substr(a,7);   i am getting the output i need.   Is there other option. Thank you

 

 

output needed;

SSS >= 1

SSS < 1

 

data one;
input a $1-14 ;
datalines;
SS-S1 SSS >= 1 
SS-S1 SSS < 1  
;

I agree with @PaigeMiller that if substr is working for you then that may be best.

 

This depends on how you are getting the data into SAS in the first place but you may be able to READ that value with an input statement. Actual content and structure of your source may allow use of @"value" column input:

 

data one;
input @"SS-S1 " a $8. ;
datalines;
SS-S1 SSS >= 1 
SS-S1 SSS < 1  
;

On the input statement an option like @"SS-S1 " tells SAS to advance the read point to the column after the end of the value in quotes and then read the variable.

 


Problem: the original data set contained SS-S1 and SS-S2

--
Paige Miller

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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
  • 7 replies
  • 2274 views
  • 3 likes
  • 3 in conversation