BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Yavuz
Quartz | Level 8
You can use compress with 'kd' argument:

Sample;
string='StudySAS Blog! 17752. ' ;
string11=compress(string,'','kd') ;

Result:
string11=17752
art297
Opal | Level 21

The following worked for me:

 

libname thedata '/folders/myfolders';

DATA EXMED_NOTMULTI_3;
  set thedata.exmed_test1 (rename=(distancetestmarche6minfinprogram=_d));
  distancetestmarche6minfinprogram=input(compress(_d,'kd'),12.);
run;

proc freq data=EXMED_NOTMULTI_3 ;
  tables distancetestmarche6minfinprogram;
  where not missing(distancetestmarche6minfinprogram);
run;

Art, CEO, AnalystFinder.com

 

seltonsy
Quartz | Level 8

Hello,

 

Thanks for the reply,

 

I tried it but it did not work (problem with INPUT) . I find it weird. here is the log.

 


407 DATA EXMED_test2;
408 set exmed_test1 (rename=(distancetestmarche6minfinprogram=_d));
409 distancetestmarche6minfinprogram=input(compress(_d,'kd'),12.);
410 run;

NOTE: Invalid argument to function INPUT at ligne 409 colonne 36.
_d=Inc Fumeacutellementadmission=Dans passe ID=2 distancetestmarche6minfinprogram=. _ERROR_=1
_N_=2
NOTE: Invalid argument to function INPUT at ligne 409 colonne 36.
_d=n/a Fumeacutellementadmission=Jamais ID=16 distancetestmarche6minfinprogram=. _ERROR_=1
_N_=16
NOTE: Invalid argument to function INPUT at ligne 409 colonne 36.
_d=Inc Fumeacutellementadmission=Dans passe ID=85 distancetestmarche6minfinprogram=. _ERROR_=1
_N_=85
NOTE: Invalid argument to function INPUT at ligne 409 colonne 36.
_d=Inc Fumeacutellementadmission=Dans passe ID=92 distancetestmarche6minfinprogram=. _ERROR_=1
_N_=92
NOTE: Invalid argument to function INPUT at ligne 409 colonne 36.
_d=Inc Fumeacutellementadmission=Jamais ID=116 distancetestmarche6minfinprogram=. _ERROR_=1
_N_=116
NOTE: Invalid argument to function INPUT at ligne 409 colonne 36.
_d=Inc Fumeacutellementadmission=Jamais ID=133 distancetestmarche6minfinprogram=. _ERROR_=1
_N_=133
NOTE: Invalid argument to function INPUT at ligne 409 colonne 36.
_d=Inc Fumeacutellementadmission=Dans passe ID=141 distancetestmarche6minfinprogram=. _ERROR_=1
_N_=141
NOTE: Invalid argument to function INPUT at ligne 409 colonne 36.
_d=Inc Fumeacutellementadmission=Jamais ID=155 distancetestmarche6minfinprogram=. _ERROR_=1
_N_=155
NOTE: Invalid argument to function INPUT at ligne 409 colonne 36.
_d=Inc Fumeacutellementadmission=Jamais ID=159 distancetestmarche6minfinprogram=. _ERROR_=1
_N_=159
NOTE: Invalid argument to function INPUT at ligne 409 colonne 36.
_d=Inc Fumeacutellementadmission=Presentemen ID=161 distancetestmarche6minfinprogram=. _ERROR_=1
_N_=161
NOTE: Invalid argument to function INPUT at ligne 409 colonne 36.
_d=Inc Fumeacutellementadmission=Jamais ID=163 distancetestmarche6minfinprogram=. _ERROR_=1
_N_=163
NOTE: Invalid argument to function INPUT at ligne 409 colonne 36.
_d=Inc Fumeacutellementadmission=Presentemen ID=165 distancetestmarche6minfinprogram=. _ERROR_=1
_N_=165
NOTE: Invalid argument to function INPUT at ligne 409 colonne 36.
_d=Unk Fumeacutellementadmission=Dans passe ID=172 distancetestmarche6minfinprogram=. _ERROR_=1
_N_=172
NOTE: Invalid argument to function INPUT at ligne 409 colonne 36.
_d=Inc Fumeacutellementadmission=Dans passe ID=180 distancetestmarche6minfinprogram=. _ERROR_=1
_N_=180
NOTE: Invalid argument to function INPUT at ligne 409 colonne 36.
_d=Inc Fumeacutellementadmission=Jamais ID=189 distancetestmarche6minfinprogram=. _ERROR_=1
_N_=189
NOTE: Invalid argument to function INPUT at ligne 409 colonne 36.
_d=Inc Fumeacutellementadmission=Dans passe ID=190 distancetestmarche6minfinprogram=. _ERROR_=1
_N_=190
NOTE: Invalid argument to function INPUT at ligne 409 colonne 36.
_d=Inc Fumeacutellementadmission=Dans passe ID=193 distancetestmarche6minfinprogram=. _ERROR_=1
_N_=193
NOTE: Invalid argument to function INPUT at ligne 409 colonne 36.
_d=Inc Fumeacutellementadmission=Presentemen ID=194 distancetestmarche6minfinprogram=. _ERROR_=1
_N_=194
NOTE: Invalid argument to function INPUT at ligne 409 colonne 36.
_d=n/a Fumeacutellementadmission=Jamais ID=196 distancetestmarche6minfinprogram=. _ERROR_=1
_N_=196
NOTE: Invalid argument to function INPUT at ligne 409 colonne 36.
WARNING: Limit set by ERRORS= option reached. Further errors of this type will not be printed.
_d=Inc Fumeacutellementadmission=Jamais ID=205 distancetestmarche6minfinprogram=. _ERROR_=1
_N_=205
NOTE: Mathematical operations could not be performed at the following places. The results of
the operations have been set to missing values.
Each place is given by: (Number of times) at (Line):(Column).
54 dans 409:36
NOTE: There were 410 observations read from the data set WORK.EXMED_TEST1.
NOTE: The data set WORK.EXMED_TEST2 has 410 observations and 4 variables.
NOTE: DATA statement used (Total process time):
real time 0.02 seconds
cpu time 0.03 seconds

 

art297
Opal | Level 21

Actually, it did work, but the log showed all of the records where alpha characters were shown. You can do it suppressing those errors with the following:

 

libname thedata '/folders/myfolders';

DATA EXMED_NOTMULTI_3;
  set thedata.exmed_test1 (rename=(distancetestmarche6minfinprogram=_d));
  distancetestmarche6minfinprogram=input(compress(_d,'kd'),?? 12.);
run;

proc freq data=EXMED_NOTMULTI_3 ;
  tables distancetestmarche6minfinprogram;
  where not missing(distancetestmarche6minfinprogram);
run;

Art, CEO, AnalystFinder.com

seltonsy
Quartz | Level 8

Thanks again, 

 

I'm done with one variable, one more to go,

 

the other variable  has character values that I want to convert to categories, it should be straight forward:

 

Here is what I tried,

 

IF Fumeacutellementadmission = 'Jamais' THEN SMOKING = 0 ;
IF Fumeacutellementadmission = 'Dans passe' THEN SMOKING = 1 ;
IF Fumeacutellementadmission = 'Presentemen' THEN SMOKING = 2 ;
IF Fumeacutellementadmission = 'Chaque jour' THEN SMOKING = 2 ;
IF Fumeacutellementadmission = 'Inconnu' THEN SMOKING = . ;
IF Fumeacutellementadmission = . THEN SMOKING = . ;

 

Thanks

art297
Opal | Level 21

Your code works for the second variable. Given the small size of your dataset, no reason to change it. For larger datasets, I'd simplify it by either using a select statement or preceding the 2nd thru last if statements with an else statement.

 

However, probably more work to change the code then necessary (given your small dataset), but a good practice to get into for the future.

 

Art, CEO, AnalystFinder.com

 

seltonsy
Quartz | Level 8
Hi,

Thanks for the reply,

The code was not working on my computer, until I added ELSE as you mentioned.

Great advice,

Keep the great work and have a good week-end,
art297
Opal | Level 21

Then, if you're problem is now solved, mark the post that provided the solution so that others won't waste time trying to solve an already solved problem.

 

Art, CEO, AnalystFinder.com

 

seltonsy
Quartz | Level 8
Thanks, the ?? removed the error messages as you said
seltonsy
Quartz | Level 8

Also , what about the other variable, this one has character values that I want to convert to categories, it should be straight forward:

 

Here is what I tried,

 

IF Fumeacutellementadmission = 'Jamais' THEN SMOKING = 0 ;
IF Fumeacutellementadmission = 'Dans passe' THEN SMOKING = 1 ;
IF Fumeacutellementadmission = 'Presentemen' THEN SMOKING = 2 ;
IF Fumeacutellementadmission = 'Chaque jour' THEN SMOKING = 2 ;
IF Fumeacutellementadmission = 'Inconnu' THEN SMOKING = . ;
IF Fumeacutellementadmission = . THEN SMOKING = . ;

 

Thanks

Tom
Super User Tom
Super User

Your last IF statement you are trying to compare your character variable to a numeric missing value.  This will cause SAS to try to convert your character variable to a number.  For example run this program to see the same effect.

data _null_;
  x='ABC';
  if x=. then put 'missing';
run;

So any value that did not look like a number will evaluate as being equal to numeric missing value.  The reason why adding the ELSE keyword made it work is because then you only try to execute the invalid syntax when the value does match one of the expected results.

 

If you want to test if a character variable is missing either compare it to a string that only has spaces.

  if x=' ' then put 'missing';

Or use the MISSING() function.

 if missing(x) then put 'missing';

If you want to compare it to an actual period then use quotes to make the period into a string literal and not a numeric missing value.

if x='.' then put 'missing';

So your code should look more like:

IF Fumeacutellementadmission = 'Jamais' THEN SMOKING = 0 ;
else IF Fumeacutellementadmission = 'Dans passe' THEN SMOKING = 1 ;
else IF Fumeacutellementadmission = 'Presentemen' THEN SMOKING = 2 ;
else IF Fumeacutellementadmission = 'Chaque jour' THEN SMOKING = 2 ;
else IF Fumeacutellementadmission = 'Inconnu' THEN SMOKING = . ;
else IF Fumeacutellementadmission = ' '  THEN SMOKING = . ;
else put 'Unknown value' Fumeacutellementadmission = $quote.;

 

seltonsy
Quartz | Level 8
Thanks, I appreciate your time answering this.

Regards
seltonsy
Quartz | Level 8
I just realized that it worked for this variable, even with the presence of errors messages.

Thanks,

seltonsy
Quartz | Level 8
I was wondering, does this blog allow sharing SAS data files or no? I was unable to do so, it said contents of file does not correspond with the name!?
Johnwick
Calcite | Level 5

Thank you for your help. Man Very Happy

 

Gclub

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 29 replies
  • 1704 views
  • 4 likes
  • 7 in conversation